When i'm not calling the same function in my code everything works well but when the function returns from a recursion suddenly the variable pch
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 instance when the loop acts on the string file2,file3,file4
it correctly split file2
and modifies the string to file2\\000file3,file4
but the next call to pch = strtok (NULL, ",");
renders pch
to be 0x0
. Are there things that i'm not aware of when calling recursion?
strtok()
is not reentrant. If you want use it in a recursive function you must use strtok_r()
.
See also: strtok, strtok_r
You can't call the strtok
function again before the previous execution is done - It is not reentrant.
Use its reentrant version strtok_r
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With