I am trying to use strtok() in nested loops but this is not giving me desired results,
possibly because they are using the same memory location. My code is of the form:-
char *token1 = strtok(Str1, "%");
while (token1 != NULL)
{
char *token2 = strtok(Str2, "%");
while (token2 != NULL)
{
//Do something
token2 = strtok(NULL, "%");
}
// Do something more
token1 = strtok(NULL, "%");
}
Yes,
strtok(), indeed, uses some static memory to save its context between invocations. Use a reentrant version ofstrtok(),strtok_r()instead, orstrtok_s()if you are using VS (identical tostrtok_r()).It has an additional context argument, and you can use different contexts in different loops.