4

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
JMcMinn
  • 275
  • 3
  • 10
  • That should be fine (http://ideone.com/Dty4x7); you'll need to post a complete test-case. – Oliver Charlesworth Jun 10 '13 at 00:22
  • 4
    You forgot to `#include `. But you should also be compiling with `-Wall -Werror`, as that would have told you about it... – Oliver Charlesworth Jun 10 '13 at 00:32
  • *Slaps forehead* Thank you. I had a feeling it would be something like this. I am compiling with -Wall, but I think I'll have to add -Werror now. – JMcMinn Jun 10 '13 at 00:35
  • `-Werror` simply causes all warnings to be turned into errors, preventing compilation from succeeding. It's pretty useful! I'd also recommend `-Wextra`. – Oliver Charlesworth Jun 10 '13 at 00:36
  • I see, thanks. What does -Wextra do? – JMcMinn Jun 10 '13 at 00:45
  • It enables a bunch of useful warnings that aren't covered by `-Wall`. See http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options for the full list of flags that you can use. – Oliver Charlesworth Jun 10 '13 at 00:47
  • All right, thanks again. Hm... now the line char *username = strtok_r(request, ":", &saveptr); is causing a segfault (so, presumably, all of them will). Problems, problems... – JMcMinn Jun 10 '13 at 00:50
  • Post the answer as an answer so the question doesn't show up with 0 answers even though it's been figured out. If no one does in an hour I will. – xaxxon Jun 10 '13 at 01:07
  • @JMcMinn if it's segfaulting still, request is probably not a valid pointer or it's not null terminated. – xaxxon Jun 10 '13 at 01:09
  • `char returncode[80]` is local variable. and `char *req = "user:serv:5:8:1:user:serv:secret1";` read only in most cases. – BLUEPIXY Jun 10 '13 at 09:28
  • Right, I see... C is such a pain for someone who was never taught it properly XP – JMcMinn Jun 10 '13 at 23:27

2 Answers2

10

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 ( . . . );

JMcMinn
  • 275
  • 3
  • 10
0

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

Ambika
  • 11