I am having a problem with my program in which I am getting a segmentation fault from strsep()
which was gotten from GDB and has the error message
Program received signal SIGSEGV, Segmentation fault.
0x00002aaaaad64550 in strsep () from /lib64/libc.so.6
My code is as follows:
int split(char *string, char *commands, char *character) {
char **sp = &string;
char *temp;
temp = strdup(string);
sp = &temp;
for (int i = 0; i < 100; i++) {
commands[i] = strsep(sp, character);
if (commands[i] == '\0') {
return 0;
}
if (strcasecmp(commands[i], "") == 0) {
i--;
}
printf("%d", i);
}
return 0;
}
Any help would be greatly appreciated as I have spent hours trying to solve this problem
The arguments for the function are ("Hello World", "@", "&")
EDIT
So I have managed to get rid of the segment fault by changing the code to
int split(char* string, char* commands, char* character) {
for(int i = 0; i < 100; i++) {
commands[i] = strsep(&string, character);
if(commands[i] == '\0') {
return 0;
}
if(strcasecmp(&commands[i], "") == 0) {
i--;
}
}
return 0;
}
However now I have a new problem with commands returning a null array where every index is out of bounds.
EDIT 2
I should also clarify on what I am trying to do a bit so essentially commands is of type char* commands[100]
and I want to pass it into the function when then modifies the original pointer array and store say `"Hello World"' into commands[0] then I want to modify this value outside the function.
Your usage of
commands
is inconsistent with the function prototype: the caller passes an array of 100char*
,commands
should be a pointer to an array ofchar *
, hence a typechar **commands
orchar *commands[]
. For the caller to determine the number of tokens stored into the array, you should either store aNULL
pointer at the end or return this number or both.Storing
commands[i] = strsep(...)
is incorrect ascommands
is defined as achar *
, not achar **
.It is surprising you get a segmentation fault in
strsep()
because the arguments seem correct, unlesscharacter
happens to be an invalid pointer.Conversely you have undefined behavior most likely resulting in a segmentation fault in
strcasecmp(commands[i], "")
ascommands[i]
is achar
value, not a valid pointer.Here is a modified version:
The memory allocated for the tokens can be freed by freeing the first pointer in the
commands
array. It might be simpler to duplicate these tokens so they an be freed in a more generic way.