strtok(3) Library Functions Manual strtok(3) strtok, strtok_r - () Standard C library (libc, -lc) #include char *strtok(char *_Nullable restrict str, const char *restrict delim); char *strtok_r(char *_Nullable restrict str, const char *restrict delim, char **restrict saveptr); glibc (. feature_test_macros(7)): strtok_r(): _POSIX_C_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE strtok() . strtok() str. , , str NULL. delim , . delim . strtok() , null, . -. , strtok() NULL. strtok(), , , , . strtok() . str . , . , strtok() NULL ( NULL strtok()). The end of each token is found by scanning forward until either the next delimiter byte is found or until the terminating null byte ('\0') is encountered. If a delimiter byte is found, it is overwritten with a null byte to terminate the current token, and strtok() saves a pointer to the following byte; that pointer will be used as the starting point when searching for the next token. In this case, strtok() returns a pointer to the start of the found token. , - , - . , , strtok() -- . , , <>, strtok() <<;,>> <> <>, null. The strtok_r() function is a reentrant version of 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 (but see VERSIONS). In subsequent calls, str should be NULL, and saveptr (and the buffer that it points to) should be unchanged since the previous call. strtok_r() saveptr. strtok() strtok_r() NULL, . attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |strtok() | | MT-Unsafe race:strtok | +----------------------------+----------------------------------------------------------+--------------------------+ |strtok_r() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ *saveptr NULL strtok_r(), str. strtok() C11, POSIX.1-2008. strtok_r() POSIX.1-2008. strtok() POSIX.1-2001, C89, SVr4, 4.3BSD. strtok_r() POSIX.1-2001. . , : o . o -. o -. o strtok() , . strtok_r() . , , , strtok_r() . . ()- , <<>> . ()- , <<>> . : $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/' 1: a/bbb///cc --> a --> bbb --> cc 2: xxx --> xxx 3: yyy --> yyy #include #include #include int main(int argc, char *argv[]) { char *str1, *str2, *token, *subtoken; char *saveptr1, *saveptr2; int j; if (argc != 4) { fprintf(stderr, "Usage: %s string delim subdelim\n", argv[0]); exit(EXIT_FAILURE); } for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) { token = strtok_r(str1, argv[2], &saveptr1); if (token == NULL) break; printf("%d: %s\n", j, token); for (str2 = token; ; str2 = NULL) { subtoken = strtok_r(str2, argv[3], &saveptr2); if (subtoken == NULL) break; printf("\t --> %s\n", subtoken); } } exit(EXIT_SUCCESS); } , strtok(), getaddrinfo_a(3). memchr(3), strchr(3), string(3), strpbrk(3), strsep(3), strspn(3), strstr(3), wcstok(3) () Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux man-pages 6.12 23 2024 . strtok(3)