Possible Duplicate:
strtok giving Segmentation Fault
Why do i get segfault using this code ?
void test(char *data)
{
char *pch;
pch = strtok(data, " ,.-"); // segfault
while (pch != NULL)
{
printf("%s\n", pch);
pch = strtok(NULL, " ,.-");
}
return NULL;
}
char *data = "- This, a sample string.";
test(data);
strtok()modifies the original string. You are passing it a constant source string that cannot be modified.Try this instead: