从递归返回后,c strtok返回NULL

Tom*_*Tom 6 c recursion strtok

当我没有在我的代码中调用相同的函数时,一切都运行良好,但是当函数突然从递归返回时,变量pch为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, ",");
        }
      }
Run Code Online (Sandbox Code Playgroud)

因此,例如当循环作用于字符串时,file2,file3,file4它正确地拆分file2并修改字符串,file2\\000file3,file4但下一次调用pch = strtok (NULL, ",");渲染pch0x0.在调用递归时是否有我不知道的事情?

Ola*_*che 11

strtok()不是可重入的.如果你想在递归函数中使用它,你必须使用它strtok_r().

另见:strtok,strtok_r


cod*_*ict 5

strtok在上一次执行完成之前,您无法再次调用该函数 - 它不是可重入的.

请改用其可重入版本strtok_r.