Strtok on 64-bit machines

The following code works differently on 64-bit and 32-bit, which causes me problems with porting 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," "))); 

The following is the conclusion:

 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 strtok man page 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. 

char * on a 64-bit machine is supposed to print 8 bytes. So why does strtok return a pointer to 4 char bytes on a 64 bit machine?

thanks

+7
source share
2 answers

You forgot #include <string.h> .

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

This solves the problem for me on gcc. If this is not for you, what compiler are you using?

+9
source

Calling strtok(tmp, " ") will lead to undefined behavior because it will try to change the string literal tmp points to, but since the sizeof operand is not evaluated (with one exception, apply here), this is not a problem.

The real problem is that you are trying to print the size_t values ​​with the format "%ld" , which requires an unsigned long argument.

If your implementation supports it, the correct format for the size_t argument is "%zu" (added in C99):

 printf("size of char * = %zu and size of strtok return val = %zu\n", sizeof(char *), sizeof(strtok(tmp," "))); 

Otherwise, explicitly convert the arguments to the appropriate size. I would use "%lu" since size_t is an unsigned type.

 printf("size of char * = %lu and size of strtok return val = %lu\n", (unsigned long)sizeof(char *), (unsigned long)sizeof(strtok(tmp," "))); 

Here's a complete stand-alone program that should produce the expected results with any implementation of C89 or later:

 #include <stdio.h> #include <string.h> int main(void) { char * tmp = "How are you?"; printf("size of char * = %lu and size of strtok return val = %lu\n", (unsigned long)sizeof(char *), (unsigned long)sizeof(strtok(tmp," "))); return 0; } 

EDIT: An OP comment on another answer indicates that the string.h header was a problem; apparently he

 #include "string.h" 

but not

 #include <string.h> 

I am going to leave this answer here because it describes another problem that needs to be fixed in the OP code, but not the one that caused the observed symptom. and the compiler took the wrong header file string.h .

+3
source

All Articles