Everything

strtok, strtok_r


Token division

[Classification]

Standard library

[Syntax]

#include <string.h>

char __far * __far strtok(char __far *s1, const char __far *s2); (C90)

char __far * __far strtok(char __far * restrict s1, const char __far * restrict s2); (C99) [V1.07 or later]

char __far * __far strtok_r(char __far *s1, const char __far *s2, char __far * __far * tokpp); (C90) [V1.16 or later]

char __far * __far strtok_r(char __far * restrict s1, const char __far * restrict s2, char __far * __far * restrict tokpp); (C99) [V1.16 or later]

[Return value]

Returns a pointer to a token. If a token does not exist, the null pointer is returned.

[Description]

This function divides the character string indicated by s1 into strings of tokens by delimiting the character string with a character in the character string indicated by s2. If this function is called first, s1 is used as the first argument. Then, calling with the null pointer as the first argument continues. The delimiting character string indicated by s2 can differ on each call.

On the first call, the character string indicated by s is searched for the first character not included in the delimiting character string indicated by s2. If such a character is not found, a token does not exist in the character string indicated by s1, and strtok returns the null pointer. If a character is found, that character is the beginning of the first token.

After that, strtok searches from the position of that character for a character included in the delimiting character string at that time. If such a character is not found, the token is expanded to the end of the character string indicated by s1, and the subsequent search returns the null pointer. If a character is found, the subsequent character is overwritten by the null character (\0) indicating the termination of the token. strtok() saves the pointer indicating the subsequent character to a static variable. strtok_r() saves it to the third argument tokpp. The next search for a token starts from there.

In the second or subsequent call with the null pointer as the first argument, the search starts from where the retained pointer indicates. Therefore, strtok() is not reentrant.

[Caution]

A null pointer check for the second argument is not performed. If a null pointer is input for the second argument, it is assumed to have no delimiting characters, and the value of the first argument is returned as a token.

For strtok_r(), a null pointer check for the third argument is not performed. If a null pointer is input for the third argument, correct operation is not guaranteed.