c strtok 从递归返回后返回 NULL [英] c strtok returns NULL after return from recursion

查看:73
本文介绍了c strtok 从递归返回后返回 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我没有在代码中调用同一个函数时,一切正常,但是当函数突然从递归返回时,变量 pch 为 NULL:

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, ",");
        }
      }

例如,当循环作用于字符串 file2,file3,file4 时,它会正确拆分 file2 并将字符串修改为 file2\\000file3,file4 但下一次调用 pch = strtok (NULL, ",");pch 呈现为 0x0.调用递归时是否有我不知道的事情?

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() 不可重入.如果你想在递归函数中使用它,你必须使用 strtok_r().

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

另见:strtok, strtok_r

这篇关于c strtok 从递归返回后返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆