What's the difference between strtok and strtok_r in C?

后端 未结 4 1873
忘掉有多难
忘掉有多难 2021-02-13 04:29

What\'s the difference between strtok and strtok_r in C and when are we supposed to use which?

4条回答
  •  情话喂你
    2021-02-13 05:09

    According the documentation, the strtok_r() function is a reentrant version of strtok().

    char *strtok_r(char *s1, const char *s2, char **s3);
    

    It gets the next token from string s1, where tokens are strings separated by characters from s2. To get the first token from s1, strtok_r() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with a null pointer for the first parameter. The string of delimiters, s2, can differ from call to call.

提交回复
热议问题