strtok problem in calling

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

    You say:

    And i have a call f1("abc,def");

    That call is illegal - strtok modifies its first parameter and you are not allowed to modify string literals. What you get is undefined behaviour - anything could happen. You want:

    char a[] = "abc,def";
    f1( a );
    

提交回复
热议问题