当我在 Linux x64 下编译我的代码时(在 x86 下没有警告)我收到以下警告 warning: format '%llx' expects argument of type 'long long unsigned int *', but argument 3 has type ' off64_t *' [-Wformat]
我的代码片段:
if(maps && mem != -1) {
char buf[BUFSIZ + 1];
while(fgets(buf, BUFSIZ, maps)) {
off64_t start, end;
sscanf(buf, "%llx-%llx", &start, &end);
dump_region(mem, start, end);
}
}
我应该如何转换才不会收到警告?
编辑:
我应该这样投吗?:
sscanf(buf, "%llx-%llx", (long long unsigned int *)&start, (long long unsigned int *)&end);
最佳答案
想到使用 sscanf()
读取非标准整数类型(如 off64_t
)的 2 种方法。
1) 尝试通过各种条件(#if ...
)预测正确的格式说明符并使用sscanf()
。假设是下面的SCNx64
#include <inttypes.h>
off64_t start, end;
if (2 == sscanf(buf, "%" SCNx64 "-%" SCNx64, &start, &end)) Success();
2) 使用 sscanf()
和最大的 int 然后转换。
#include <inttypes.h>
off64_t start, end;
uintmax_t startmax, endmax;
if (2 == sscanf(buf, "%" SCNxMAX "-%" SCNxMAX, &startmax, &endmax)) Success();
start = (off64_t) startmax;
end = (off64_t) endmax;
// Perform range test as needed
if start != startmax) ...
顺便说一句:对于scanf()
,建议使用PRI...
应该是SCN...
。 PRI...
适用于 printf()
系列。
检查 sscanf()
结果总是好的。
关于c - 警告 : format ‘%llx’ expects argument of type ‘long long unsigned int *’ , 但参数 3 的类型为 ‘off64_t *’ [-Wformat],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21575164/