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

后端 未结 4 1870
忘掉有多难
忘掉有多难 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:16

    strtok is equivalent to (and often defined as):

    char *strtok(char *str, const char *delim) {
        static char *save;
        return strtok_r(str, delim, &save);
    }
    

    in general, you should use strtok_r directly rather than strtok, unless you need to make your code portable to pre-POSIX-2001 systems that only support strtok

提交回复
热议问题