c - 警告 : format ‘%llx’ expects argument of type ‘long long unsigned int *’ , 但参数 3 的类型为 ‘off64_t *’ [-Wformat]

标签 c linux 64-bit

当我在 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/

相关文章:

linux - 堆的边界是什么?

c++ - 一劳永逸地理解 C 和 C++ 中 f() 和 f(void) 之间的区别

将二维数组转换为一维数组(有条件)

arrays - 使用 plistBuddy 获取值数组

linux - 适用于 ARM 的 Tizen IVI 3.0 移植

linux - 在Ubuntu Bionic 18.04 Xeon上安装docker

c - setitimer 的 ITIMER_PROF(SIGPROF) 是否发送到多线程和 NPTL 以及 Linux(2.6.21.7) 中的每个线程?

c - 变量类型结构体是否必须是数组才能存储 100 个元素?

c - mmap()用于远程文件

javascript - Javascript 对 64 位浮点的 native 支持