i try to implement a split function to split a string with a caracter like in java. But my function need to stock the splited string in tab with 2 dimensions (the tab is in parameters). And my function need to return the numbers of arg.
So i pass an address of char ** at my function and i want to realloc this char ** But it doesn't work.
int split(char*** parsedCommand,const char* splitCaracter,const char* myString) {
char* splittedPart = "";
char* copyOfmyString = NULL;
int argc = 0;
copyOfmyString = strdup(myString);
splittedPart = strtok(copyOfmyString, splitCaracter);
while(splittedPart != NULL)
{
*parsedCommand = (char**) realloc(*parsedCommand, sizeof(char*)*(argc+1));
if (*parsedCommand == NULL)
{
printf("fatalError ");
exit(-1);
}
(*parsedCommand)[argc] = strdup(splittedPart);
if ((*parsedCommand)[argc] == NULL)
{
printf("fatalError ");
exit(-1);
}
splittedPart = strtok (NULL, splitCaracter);
argc++;
}
free(copyOfmyString);
return argc;
}
I have an Segmentation fault when i try to split a string with 2 arg like "ls | ls", the first ls was stock well in the tab, but for the second one we gat an segmentation falt at strdup(splittedPart).
I hope you understand my English, i'm not really good at it. Thx.
EDIT : i forgot i call the function with :
char ** cmd_membres = NULL;
split(&cmd_membres, "|", chaine);