64 位机器上的 strtok

发布于 2025-01-07 15:59:56 字数 724 浏览 0 评论 0原文

以下代码在 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," ")));

以下是输出:

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 个字节。那么为什么 strtok 在 64 位机器上返回 4 字节 char 指针呢?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女七分熟 2025-01-14 15:59:56

您忘记了#include

这导致编译器推断出默认返回类型 int。通过 #include 正确的头文件,正确的原型被拉入范围。

这解决了我在 gcc 上的问题。如果不适合您,您使用什么编译器?

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?

短叹 2025-01-14 15:59:56

调用 strtok(tmp, " ") 会导致未定义的行为,因为它会尝试修改 tmp 指向的字符串文字 - 但由于 的操作数sizeof 不会被评估(有一个例外,此处不适用),这不是问题。

真正的问题是您尝试使用 "%ld" 格式打印 size_t 值,这需要 unsigned long 参数。

如果您的实现支持它,则 size_t 参数的正确格式为 "%zu"(在 C99 中添加):

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

否则,将参数显式转换为适当的大小。我会使用 "%lu",因为 size_t 是无符号类型。

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

这是一个完整的独立程序,应该在任何 C89 或更高版本的实现上产生预期结果:

#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;
}

编辑:
OP 对另一个答案的评论表明 string.h 标头是问题所在;显然是他

#include "string.h"

而不是

#include <string.h>

我将这个答案留在这里,因为它描述了需要在OP代码中修复的另一个问题,尽管不是导致观察到的症状的问题。
并且编译器选择了错误的 string.h 头文件。

Calling strtok(tmp, " ") would cause undefined behavior, since it would attempt to modify the string literal that tmp points to -- but since the operand of sizeof is not evaluated (with one exception that doesn't apply here), that's not an issue.

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

If your implementation supports it, the correct format for a 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'd 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 self-contained program that should produce the expected results on any C89 or later implementation:

#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 :
The OP's comment on the other answer indicates that the string.h header was the problem; apparently he had

#include "string.h"

rather than

#include <string.h>

I'm going to leave this answer here because it describes another problem that needs to be fixed in the OP's code, though not the one that caused the observed symptom.
and the compiler picked up the wrong string.h header file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文