Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtok on 64 bit machines

The following code works differently on 64 bit and on 32 bit which is causing me trouble to port my code.

char * tmp = "How are you?";
printf("size of char * = %ld and size of strtok return val = %ld \n",sizeof(char *),sizeof(strtok(tmp," ")));

Following is the output:

32 bit: 
size of char * = 4 and size of strtok return val = 4 

64 bit:

size of char * = 8 and size of strtok return val = 4

The man page of strtok says:

   #include <string.h>

   char *strtok(char *str, const char *delim);

RETURN VALUE
       The strtok() and strtok_r() functions return a pointer to the next token, or NULL if there are no more tokens.

The char* on a 64 bit machine is supposed to be 8 bytes as printed. So why is strtok returning a 4 bytes char pointer on a 64 bit machine??

Thanks

like image 371
ntalli Avatar asked Feb 21 '12 00:02

ntalli


1 Answers

You forgot to #include <string.h>.

This is causing the default return type of int to be inferred by the compiler. By #including the right header file, the correct prototype is pulled into scope.

This solves the problem for me on gcc. If it doesn't for you, what compiler are you using?

like image 64
Clark Gaebel Avatar answered Sep 20 '22 21:09

Clark Gaebel