Strtok behaves inconsistently

I try to read data from a file, split it into tokens and sort it, however it is strtokwrong when I run it, sometimes it works, sometimes not, and I get very short / strange tokens.

Valgrind seems to think that this is because strtok relies on an uninitialized value (I think):

==7069== Conditional jump or move depends on uninitialised value(s)  
==7069==    at 0x40B61A3: strtok (strtok.S:160)  
==7069==    by 0x8048842: main (main.c:58)

Here is the function that I think Walgrind accuses:

char * getNextToken(char * line) {  
    char delim = ',';  
    return strtok(line, &delim);  
}

Could this be due to the fact that for most of my calls in strtokline NULL?

Here are my function calls:

strcpy(performer, getNextToken(inputLine));  
strcpy(title, getNextToken(NULL));  
strcpy(charMin, getNextToken(NULL));  
/*etc...*/

I have no idea what could be causing this, and all the values ​​that I give strtokare what I expect. Also, I sometimes get a stack crash error, I don’t know why.

+4
source share
2

strtok . . :

char * getNextToken (char * line){
    const char *delim = ",";
    return strtok(line, delim);
}
+7

strtok(). , , " ", , strtok , , , strcpy(), .

strdup() strcpy().

+1

All Articles