strtok problem in calling

前端 未结 4 987
醉话见心
醉话见心 2021-01-24 15:27

I have a function using strtok like this

void f1(char *name)
{
...
char *tmp;
tmp = strtok(names, \" ,\");
while(tmp)
{
...
tmp = strtok(NULL, \" ,\");
}
...
}
<         


        
4条回答
  •  离开以前
    2021-01-24 16:25

    Are you truly passing in a string literal?

    f1("abc,def");
    

    will pass a pointer to the string literal to the strtok() in f1() - since strtok() modifies the string, and a string literal can't be modified, so you'll get undefined behavior (though I would expect a crash or fault instead of unexpected results).

提交回复
热议问题