Search code examples
cstrtok

strtok return int type in c


When I run this code it gave a segmentation fault.

main ()
{
    char *line ="2,3,22";
    int size=3,i,j; 
    double value; 
    char *val;

    for (i = 0; i<size ; i++) {
        if (line == NULL) break;
        printf("%s\n",strtok (line ,","));
    }
}

and gave a

"warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
 printf("%s\n",strtok (line ,","));"

So when I assign strtok value to char* it gave an error. why strtok return an int value? What is the solution?


Solution

  • It's using an implicit prototype for strtok. You need to #include <string.h> (You might also need to include stddef.h for NULL and should also include stdio.h for printf).