Why is strtok causing a segmentation fault?

strtok modifies its first argument, hence it should be modifiable.

Maybe ReadName() returns a pointer to a read-only char array.Can you show us your ReadName() function.

If that is the reason for seg-faullt, you can create a copy of the char array before you pass it to strtok using the strdup function like:

char *copy = strdup(m);
token = strtok(copy,'-');
....
....
free(copy); // free the copy once you are done using it.

Leave a Comment