Strtok_r calls "assignment makes a pointer out of the whole without casting"

I am trying tokenize String in C and storing tokens in multiple variables using strtok_r. As far as I can tell, I use it exactly as documented:

char *saveptr; char *ticketuser = strtok_r(request, ":", &saveptr); char *ticketservice = strtok_r(NULL, ":", &saveptr); char *ticketkey = strtok_r(NULL, ":", &saveptr); //And so on... 

Where the "request" is a string of colon-separated tokens. When I try to compile, I get that “assignment makes the pointer a whole without casting” on each line, where I assign one of the lines. Since strtok_r should return char *, I don't see what the problem is.

EDIT: Here ALL is my code:

 #include <stdio.h> char *add( char *request ) { char *name = "add"; char *secret = "secret1"; char *failcode = "0:add:0"; char returncode[80]; char *saveptr; char *username = strtok_r(request, ":", &saveptr); char *servicename = strtok_r(NULL, ":", &saveptr); int parameter1 = atoi(strtok_r(NULL, ":", &saveptr)); int parameter2 = atoi(strtok_r(NULL, ":", &saveptr)); int ticketlead1 = atoi(strtok_r(NULL, ":", &saveptr)); char *ticketuser = strtok_r(NULL, ":", &saveptr); char *ticketservice = strtok_r(NULL, ":", &saveptr); char *ticketkey = strtok_r(NULL, ":", &saveptr); //Catch any issues with the request if (strcmp(username,ticketuser) != 0){ printf("username did not match ticket username\n"); return failcode; }//if else if (strcmp(servicename,ticketservice) != 0){ printf("service name did not match ticket service name\n"); return failcode; }//else if else if (strcmp(secret,ticketkey) != 0){ printf("secret key did not match ticket secret key\n"); return failcode; }//else if //request was good, return value else{ int val = parameter1 + parameter2; sprintf(returncode, "1:add:%d", val); return returncode; }//else }//add int main( int argc, char **argv ) { char *returned; char *req = "user:serv:5:8:1:user:serv:secret1"; returned = add(req); printf(returned); printf("\n"); return 1; }//main 
+4
source share
2 answers

The answer was found in the comments: I was missing #include <string.h> at the top of the file.

EDIT: I have to add that there were other issues besides those mentioned above. First, saveptr must be initialized to zero. Secondly, as BLUEPIXY noted, returncode[] was a local variable. Replaced its definition with char *returncode = malloc ( . . . );

+10
source

This can be removed using the header file "string.h" in C

0
source

Source: https://habr.com/ru/post/1485339/


All Articles