【问题标题】:warning: format ‘%llx’ expects argument of type ‘long long unsigned int *’, but argument 3 has type ‘off64_t *’ [-Wformat]警告:格式“%llx”需要“long long unsigned int *”类型的参数,但参数 3 的类型为“off64_t *”[-Wformat]
【发布时间】:2014-02-05 10:53:04
【问题描述】:

当我在 Linux x64 下编译代码时(在 x86 下没有警告),我收到以下警告 warning: format ‘%llx’ expects argument of type ‘long long unsigned int *’, but argument 3 has type ‘off64_t *’ [-Wformat]

我的代码 sn-p:

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);

【问题讨论】:

  • 使用 PRIu64 而不是 %llx
  • 检查this如何使用它是一个宏。
  • 类似sscanf(buf, "%"PRIu64"-%"PRIu64, &start, &end); 的东西?编译时没有警告,但代码现在不起作用。
  • 是的,但是缓冲区中有什么,请注意您在 scanf 中使用了-,这就是我没有发布答案的原因。 buff 应为 "3223-2254"
  • @Grijesh Chauhan 对于类型 >= sizeof(int),我希望格式相同。也许不适合较小的类型?没有方便的链接。

标签: c linux 64-bit


【解决方案1】:

想到使用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) 使用最大 int 的sscanf(),然后转换。

 #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) ...

顺便说一句:使用PRI... 的建议应该是SCN... 用于scanf()PRI... 是为printf() 家庭。

检查sscanf() 结果总是很好。

【讨论】:

  • 好主意。在这里投射到long long unsigned int 是错误的,因为它有效?
  • 不确定你指的是什么演员,在这里或有问题。我会从你的帖子中假设演员。你当然不想使用(long long unsigned int *)&amp;startstartoff64_t 类型,可能不同于 long long unsigned int。如果off64_t总是long long unsigned int 相同,它就不会存在。因此,如果 (long long unsigned int *)&amp;start 今天对您“有效”,它可能不适用于其他平台或以后的编译。
【解决方案2】:

目前看来我能找到的最好方法是强制转换:

#if __GNUC__
  #if __x86_64__ || __ppc64__
    #define ENV64BIT
    #define _LARGEFILE_SOURCE
    #define _FILE_OFFSET_BITS 64
  #else
    #define ENV32BIT
  #endif
#endif

然后

#if defined(ENV64BIT)
    sscanf(buf, "%llx-%llx", (long long unsigned int *)&start, (long long unsigned int *)&end);
#else
    sscanf(buf, "%llx-%llx", &start, &end);
#endif

【讨论】:

    猜你喜欢
    • 2015-02-27
    • 2014-02-03
    • 2021-06-22
    • 2014-01-02
    • 1970-01-01
    • 2021-05-12
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多