I am writing a function to combine strings, but when I execute I get a segmentation fault, which doesn't make sence. Segmentation fault is when you try to acess a point of memory that doesn't exist, like trying to acess array[10] when you declared array[7].
Here is the source code:
I know there are functions in the string.h header, but as you can see, I wanted to do it without them, thus calling those functions "cheating".
(sizeof string1) is a constant and will not vary depending on the contents of string1. And what if 1200 characters were entered? Where do you think the extra 200 will be? You seem to be intentionally blowing arrays.
But your problem is the combine routine. It returns a character, not a string. Or it would had you returned *anything*. Then you use this character as a pointer to character. It looks like you might mean for combine to return a pointer to character. If so, define it that way and put in a return statement.
But your problem is the combine routine. It returns a character, not a string. Or it would had you returned *anything*. Then you use this character as a pointer to character. It looks like you might mean for combine to return a pointer to character. If so, define it that way and put in a return statement.
I'm sorry, but what does that have to do with the segmentation fault?
the function is declared as returning char; however there is no return statement that actually returns a char. In this case the function will return a "garbage" value even if the syntax is not necessarily incorrect!
Thanks milhan, you explained it better. I still get a segmentation fault that I don't understand, the current source (almost same as before) :
I get an erorr related to
Which is exactly as put:
so, I assume that I could redeclare string1 as an int, to make the program compile, even though it wouldn't run correctly, because int is numbers(it can be ascii, but that adds up fast), and char is text. So I declare string1 like:
and I get a segmentaton fault after I enter the two strings. I assume that the int was the problem, so I try a few other examples:
Which produces segmentation erorr when executed.
Also produces segmentation erorr when executed.
The odd part is I don't seem to be asking for a part of memory that doesn't exist.
octal look at your combine(char [], char[]) function again. I didn't see it at my previous post either.
Quote:
Which produces segmentation erorr when executed.
return string1[0] is correct. The segmentation fault can't come from here. Try running again with the correction in the combine func. and return string1[0]..
Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1...
I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one.
Below are two very condensed snippets of which I have added the results inside the each code section.
IMPORTANT!... (11 Replies)
this is a network programming code to run a rock paper scissors in a client and server.
I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault.
-the segmentation fault is fixed
Current problem -Also... (3 Replies)
I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this :
i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b:
int main()
{
mysqlpp::Connection conn(false);
if... (3 Replies)
I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek:
#include </usr/include/mysql++/mysql++.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int outputToImport(const char*... (1 Reply)
If I do this.
Assume
struct life
{
char *nolife;
}
struct life **life;
// malloc initialization & everything
if(life->nolife == 0)
Would I get error at life->nolife if it is equal to 0.
wrong accession? (3 Replies)
Hi,
Why I don't receive a segmentation fault in the following sample.
int main(void)
{
char buff;
sprintf(buff,"Hello world");
printf("%s\n",buff);
}
If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)