C strtok returns NULL after returning from recursion

When I don't call the same function in my code, everything works fine, but when the function returns from recursion all of a sudden, the pch variable is NULL:

  void someFunction() { char * pch; char tempDependencies[100*64+100]; strcpy(tempDependencies,map[j].filesNeeded); pch = strtok(tempDependencies,","); while (pch != NULL) { someFunction(); <- if i comment this out it works fine pch = strtok (NULL, ","); } } 

So, for example, when the loop acts on the line file2,file3,file4 , it correctly splits file2 and changes the line to file2\\000file3,file4 , but the next call is pch = strtok (NULL, ","); displays pch as 0x0 . Are there things I don't know about when calling recursion?

+6
source share
2 answers

strtok() not repetitive. If you want to use it in a recursive function, you must use strtok_r() .

See also: strtok, strtok_r

+11
source

You cannot call the strtok before executing the previous execution - it is not reentrant .

Instead, use its reentrant version of strtok_r .

+5
source

All Articles