strtok_r

Breaks a string into a sequence of tokens, each of which is delimited by a character from another string.

Library:LibC
Classification:POSIX
Service:Characters and Strings

Syntax

  #include <string.h> 
   
  char *strtok_r (
     char         *s1,
     const char   *s2,
     char        **s3);
  

Parameters

s1

(IN) Points to the string to break into a sequence of tokens.

s2

(IN) Points to the string containing the delimiter characters.

s3

(IN/OUT) Points to a value that records the progress through s1.

Return Values

If a delimiter is found, returns a pointer to the first byte of a token. On subsequent iterations, if no delimiter is found, returns a NULL pointer.

If s1 does not contain any of the delimiters specified in s2, the function returns a pointer to s1, and all of s1 is considered to be a token.

Remarks

The strtok_r function is multithread safe.

The strtok_r function is used to break the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. For example, suppose that s1 and s2 contain the following characters:

  s1: "abcd:efgh:ijkl"
  s2: ":"
  

The first call to strtok _r would return a pointer to a, because abcd is the first token. The second call to strtok_r would return a pointer to e, because efgh is the second token. The second and subsequent calls to strtok_r must pass a NULL pointer as the first argument, in order to get the next token in the string. The strtok_r function saves a pointer to the following character in the s3 parameter, from which the next search for a token starts when the first argument is a NULL pointer.

The strtok_r function replaces each delimiter that it finds with a NULL character, which terminates the current token. Because strtok_r can modify the original string, that string should be duplicated if the string is to be reused.

The set of delimiters used in the calls to strtok_r can be different from one call to the next.

See Also