C: correct usage of strtok_r

前端 未结 4 1475
温柔的废话
温柔的废话 2020-11-30 07:11

How can I use strtok_r instead of strtok to do this?

char *pchE = strtok(NULL, \" \");

Now I\'m trying to use strtok_r properl

4条回答
  •  广开言路
    2020-11-30 07:58

    The documentation for strtok_r is quite clear.

    The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string.

    On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

    So you'd have code like

    char str[] = "Hello world";
    char *saveptr;
    char *foo, *bar;
    
    foo = strtok_r(str, " ", &saveptr);
    bar = strtok_r(NULL, " ", &saveptr);
    

提交回复
热议问题