c strtok returns NULL after return from recursion
Asked Answered
D

2

6

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?

Draggle answered 14/11, 2012 at 6:16 Comment(0)
A
11

strtok() is not reentrant. If you want use it in a recursive function you must use strtok_r().

See also: strtok, strtok_r

Apiece answered 14/11, 2012 at 6:18 Comment(0)
T
5

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.

Trachytic answered 14/11, 2012 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.