C: correct usage of strtok_r

101,464

Solution 1

The documentation for strtok_r is quite clear.

The strtok_r() function is a reentrant version 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. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

So you'd have code like

char str[] = "Hello world";
char *saveptr;
char *foo, *bar;

foo = strtok_r(str, " ", &saveptr);
bar = strtok_r(NULL, " ", &saveptr);

Solution 2

Tested example:

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

int main(void)
{
    char str[] = "1,22,333,4444,55555";
    char *rest = NULL;
    char *token;

    for (token = strtok_r(str, ",", &rest);
         token != NULL;
         token = strtok_r(NULL, ",", &rest)) {   
        printf("token:%s\n", token);
    }

    return 0;
}

Result.

token:1
token:22
token:333
token:4444
token:55555

Test: http://codepad.org/6xRdIecI

From linux documentation where emphasis is mine:

char *strtok_r(char *str, const char *delim, char **saveptr);

The strtok_r() function is a reentrant version 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. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.

Solution 3

I post a tested example to understand the correct usage of strtok_r() instead of using strtok() in nests.

first lets take a string "y.o.u,a.r.e,h.e.r.e" and separate using the delimiters "," and "."

using strtok()

#include<stdio.h>
#include<string.h>
int main(void) {

        char str[]="y.o.u,a.r.e,h.e.r.e";
        const char *p=",", *q=".";
        char *a,*b;

        for( a=strtok(str,p) ; a!=NULL ; a=strtok(NULL,p) ) {
                printf("%s\n",a);
                for( b=strtok(a,q) ; b!=NULL ; b=strtok(NULL,q) )
                        printf("%s\n",b);
        }

        return 0;
}

OUTPUT:

y.o.u
y
o
u

now lets use strtok_r() for same example

using strtok_r()

#include<stdio.h>
#include<string.h>
int main(void) {

        char str[]="y.o.u,a.r.e,h.e.r.e";
        const char *p=",",*q=".";
        char *a,*b,*c,*d;

        for( a=strtok_r(str,p,&c) ; a!=NULL ; a=strtok_r(NULL,p,&c) ) {
                printf("%s\n",a);

                for( b=strtok_r(a,q,&d) ; b!=NULL ; b=strtok_r(NULL,q,&d) )
                        printf("%s\n",b);
        }

        return 0;
}

OUTPUT:

y.o.u
y
o
u
a.r.e
a
r
e
h.e.r.e
h
e
r
e

therefore the strtok_r() has reentrant property whereas strtok() doesnot function like that.

Solution 4

char str[]="string for sample";
char *reserve;
char *pchE = strtok_r(str, " ", &reserve);//when next call str -> NULL
Share:
101,464
Carlcox89
Author by

Carlcox89

Updated on May 11, 2020

Comments

  • Carlcox89
    Carlcox89 about 4 years

    How can I use strtok_r instead of strtok to do this?

    char *pchE = strtok(NULL, " ");
    

    Now I'm trying to use strtok_r properly... But sometimes I get problems with the strtol. I have a thread that I execute 10 times (at the same time).

    char *savedEndd1;
    char *nomeClass = strtok_r(lineClasses, " ", &savedEndd1);
    char *readLessonS = strtok_r (NULL, " ", &savedEndd1);
    char *readNTurma = strtok_r(NULL, " ",  &savedEndd1);
    
    if (readNTurma==NULL)
    printf("CLASS STRTOL begin %s %s\n",nomeClass, readLessonS );
    int numberNTurma = strtol(readNTurma, NULL, 10);
    

    And I'm catching that readNTurma == NULL several times... Why is that? Cant understand why it comes NULL?