Hi all,
I justed started C programming, after 1.5 year PHP programming. I tried to make a explode function just like in PHP, which exlodes a string with a given separator. Example:
char = explode ("bla bla", " "); // should return:
char[0][3] = "bla";
char[1][3] = "bla";
But it doesn't.
Source:
This is the error it says:Code:#include <string.h>
#include <stdio.h>
char **func_explode (char *str, char *sep) {
printf ("str: '%s' sep: '%s'",str,sep);
char **newstr = malloc(sizeof(str));
newstr[0] = strtok (str, sep);
char *x = malloc (1024*4);
x = strtok(NULL, sep);
int i = 1;
while (x != NULL) {
newstr[i] = x;
char *x = strtok(NULL, sep);
i++;
}
return newstr;
}
int main (int argc, char **argv) {
char *zin = "Hoi, ik ben bart enzo :D";
char *sep = " ";
char nieuwzin = **func_explode (zin, sep);
return 0;
}
~/C$ make explode
cc -O2 -fno-strict-aliasing -pipe explode.c -o explode
~/C$ ./explode
Bus error: 10 (core dumped)
~/C$
Can anyone please help me?
Thank you!
Bart S.