user2847598
user2847598

Reputation: 1299

strtok() doesn't process the second token

I want to get tokens from a string, then get sub-tokens of the tokens, like this short program:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

void f(char *bak) 
{ 
    char *token, *delim = "."; 

    token = strtok(bak, delim); 
    while(token) { 
        printf("f(): token: %s\n", token); 
        token = strtok(NULL, delim); 
    } 
} 

int main(void) 
{ 
    char str[] = "a.1.2 x.y"; 
    char *token, *delim = " \t\n\r"; 

    token = strtok(str, delim); 
    while(token) { 
        printf("main: token: %s\n", token); 

        char bak[100]; 
        strncpy(bak, token, sizeof(bak)); 
        f(bak); 

        token = strtok(NULL, delim); 
    } 

    return 0; 
}

However, it only shows the first token ("a.1.2"), not the second one:

main: token: a.1.2
f(): token: a
f(): token: 1
f(): token: 2

How did this happen? Thanks.

Upvotes: 2

Views: 1089

Answers (2)

Ritesh Prajapati
Ritesh Prajapati

Reputation: 983

strtok() handles one token for one string at a time. So, You can use strtok_r() function to handle multiple token at a time for one string.

SO, Its a recursive function of strtok() function.

Upvotes: 0

neverhoodboy
neverhoodboy

Reputation: 1050

strtok() can only handle the tokenization of one string at a time (it depends on an internal static variable to maintain states between successive calls, non-reentrant and non-threadsafe). The call to strtok(bak, delim) in f() invalidates the previous call to strtok(str, delim) in main(), so when the execution flow returns back to main() from f() and comes to the call to strtok(NULL, delim), it's actually still working on the tokenization of "a.1.2" (which is already finished in f()), thus token is assigned with a null pointer value, which terminates the loop.

Upvotes: 4

Related Questions