strtok problem in calling

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

    strtok() modifies its input string by overwriting the delimiters with a 0; so, assuming your code looks something like this:

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

    after the first call to f1, the ',' character is overwritten with a 0, which is a string terminator, so the second call only sees "abc" as the string.

    Note that because strtok() modifies its input, you do not want to pass it a string literal as an argument; attempting to modify the contents of a string literal invokes undefined behavior.

    The safe thing to do is to create a local string within f1 and copy the contents of names to it, then pass that local string to strtok(). The following should work with C99:

    void f1(char *name)
    {
      size_t len = strlen(name);
      char localstr[len+1];
      char *tmp;
      strcpy(localstr, name);
    
      tmp = strtok(localstr, " ,");
      while(tmp)
      {
        ...
        tmp = strtok(NULL, " ,");
      }
    }
    

提交回复
热议问题