strtok problem in calling

前端 未结 4 989
醉话见心
醉话见心 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:07

    strtok puts a null terminator after each token it returns. This means that it destroys the original string: After calling it, your string will be terminated after the first token, resulting in the behavior you see.

    To keep the original string unchanged, you will need to make a copy of it before calling strtok.

提交回复
热议问题