nta*_*lli 8 c strtok 32bit-64bit
以下代码在64位和32位上的工作方式不同,这使我无法移植代码.
char * tmp = "How are you?";
printf("size of char * = %ld and size of strtok return val = %ld \n",sizeof(char *),sizeof(strtok(tmp," ")));
Run Code Online (Sandbox Code Playgroud)
以下是输出:
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
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
64位机器上的char*应该是打印的8个字节.那么为什么strtok在64位机器上返回一个4字节的char指针?
谢谢
Cla*_*bel 10
你忘了#include <string.h>.
这导致int编译器推断出默认返回类型.通过#include正确的头文件,将正确的原型拉入范围.
这解决了我在gcc上遇到的问题.如果它不适合你,你使用什么编译器?