I'm attempting to tokenize a String in C and save the tokens into multiple variables using strtok_r. As far as I can tell, I'm using 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 'request' is a String of colon-delimited tokens. When I try to compile, I get "assignment makes pointer from integer without a cast" on each line where I assign one of the Strings. Since strtok_r is supposed to return a char*, I can't see what the issue is.
EDIT: Here's ALL 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
                Answer was found in comments: I was missing #include <string.h> at the top of the file.
EDIT: I should add that there were other issues besides the one mentioned above. Firstly, saveptr should be initialized to null. Secondly, as BLUEPIXY pointed out, returncode[] was a local variable. Replaced its definition with char *returncode = malloc ( . . . );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With