'\" t .\" Copyright 2005-2013, Michael Kerrisk .\" Copyright, the authors of the Linux man-pages project .\" .\" SPDX-License-Identifier: Linux-man-pages-copyleft .\" .TH strtok_r 3 2026-02-25 "Linux man-pages 6.19" .SH NAME strtok_r \- string tokenize reentrant .SH LIBRARY Standard C library .RI ( libc ,\~ \-lc ) .SH SYNOPSIS .nf .B #include .P .BI "char *strtok_r(char *_Nullable restrict " str \ ", const char *restrict " delim , .BI " char **restrict " saveptr ); .fi .P .RS -4 Feature Test Macro Requirements for glibc (see .BR feature_test_macros (7)): .RE .P .BR strtok_r (): .nf _POSIX_C_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE .fi .SH DESCRIPTION The .BR strtok_r () function is a reentrant version of .BR strtok (3). .P The .I saveptr argument is a pointer to a .I char\~* variable that is used internally by .BR strtok_r () in order to maintain context between successive calls that parse the same string. .P On the first call to .BR strtok_r (), .I str should point to the string to be parsed, and the value of .I *saveptr is ignored (but see VERSIONS). In subsequent calls, .I str should be NULL, and .I saveptr (and the buffer that it points to) should be unchanged since the previous call. .P Different strings may be parsed concurrently using sequences of calls to .BR strtok_r () that specify different .I saveptr arguments. .SH RETURN VALUE .BR strtok_r () returns a pointer to the next token, or NULL if there are no more tokens. .SH ATTRIBUTES For an explanation of the terms used in this section, see .BR attributes (7). .TS allbox; lbx lb lb l l l. Interface Attribute Value T{ .na .nh .BR strtok_r () T} Thread safety MT-Safe .TE .SH VERSIONS On some implementations, .\" Tru64, according to its manual page .I *saveptr is required to be NULL on the first call to .BR strtok_r () that is being used to parse .IR str . .SH STANDARDS POSIX.1-2008. .SH HISTORY POSIX.1-2001. .SH EXAMPLES The program below uses nested loops that employ .BR strtok_r () to break a string into a two-level hierarchy of tokens. The first command-line argument specifies the string to be parsed. The second argument specifies the delimiter byte(s) to be used to separate that string into "major" tokens. The third argument specifies the delimiter byte(s) to be used to separate the "major" tokens into subtokens. .P An example of the output produced by this program is the following: .P .in +4n .EX .RB "$" " ./a.out \[aq]a/bbb///cc;xxx:yyy:\[aq] \[aq]:;\[aq] \[aq]/\[aq]" 1: a/bbb///cc \-\-> a \-\-> bbb \-\-> cc 2: xxx \-\-> xxx 3: yyy \-\-> yyy .EE .in .SS Program source \& .\" SRC BEGIN (strtok.c) .EX #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\[rs]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\[rs]n", j, token); \& for (str2 = token; ; str2 = NULL) { subtoken = strtok_r(str2, argv[3], &saveptr2); if (subtoken == NULL) break; printf("\[rs]t \-\-> %s\[rs]n", subtoken); } } \& exit(EXIT_SUCCESS); } .EE .\" SRC END .SH SEE ALSO .BR strtok (3)