以下代码在64位和32位上的工作方式不同,这使得我移植代码时遇到了麻烦。
以下是输出结果:
在64位机器上,char*应该打印为8个字节。那么为什么在64位机器上,strtok返回一个4个字节的char指针呢?谢谢。
char * tmp = "How are you?";
printf("size of char * = %ld and size of strtok return val = %ld \n",sizeof(char *),sizeof(strtok(tmp," ")));
以下是输出结果:
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
strtok的手册说:
#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.
在64位机器上,char*应该打印为8个字节。那么为什么在64位机器上,strtok返回一个4个字节的char指针呢?谢谢。
<string.h>
库?那么编译器可能会感到有些传统,对于它不知道的函数假定返回类型为int
。 - Daniel Fischergcc-4.5.real (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
上确认。太疯狂了。 - sarnold<string.h>
时都显示为8,在没有使用时分别为8和4。 - Daniel Fischer