gpt4 book ai didi

C++ printf 字段宽度说明符 ‘.*’ 需要 int 而不是 size_t

转载 作者:太空狗 更新时间:2023-10-29 21:37:25 25 4
gpt4 key购买 nike

我正在修复我继承的遗留项目中的编译器警告。新的编译器是 gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)。

他们有很多像下面这样的代码:

#include <cstdio>
#include <cstring>

struct foobar
{
char field1[10];
char field2[5];
};

int main()
{
struct foobar foo;
memset(&foo, ' ', sizeof(foo));
strncpy(foo.field1, "1234567890", sizeof(foo.field1));

// Produces warning
printf("[%.*s]", sizeof(foo.field1), foo.field1);

return 0;
}

这会产生一条警告消息:

1_test.c: In function ‘int main()’:
1_test.c:16:49: warning: field precision specifier ‘.*’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("[%.*s]", sizeof(foo.field1), foo.field1);

这对我来说似乎是错误的,因为 '.*' 应该期待一个 size_t 但显然它不会......

除了必须执行以下操作之外,是否还有全局解决此问题的方法:

    // Fixes
printf("[%.10s]", foo.field1);

// Fixes
printf("[%.*s]", static_cast<int>(sizeof(foo.field1)), foo.field1);

最佳答案

正确的解决方案是:

std::cout << std::string( foo.field1, sizeof( foo.field1 ) );

这将输出您想要的内容并且不会生成警告。但更好的解决方案当然是在 struct foobar

中使用 std::string

关于C++ printf 字段宽度说明符 ‘.*’ 需要 int 而不是 size_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37753132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com