【问题标题】:warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’警告:格式“%d”需要类型“int”,但参数 4 的类型为“size_t”
【发布时间】:2013-04-22 12:18:45
【问题描述】:

我按照一些类似的帖子修改了代码,但是仍然会生成警告。

$ g++ ncfile.c -o ncfile -g -lnetcdf

ncfile.c: In function ‘int main(int, char**)’:
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’

在第 363 行和第 364 行附近的那个街区:

  for(i = 0; i < ndimsp; i++) {
    char * temp_name;
    size_t temp_len;
    temp_name = (char *)malloc(sizeof(char) * 20);
    nc_inq_dim(cid, dimids[i], temp_name, &temp_len);
    dimlens[i] = temp_len;
    if(dimids[i] == unlimdimidp) printf("\t\t%d %s   \tlength: %d (UNLIMITED)\n", i, temp_name, temp_len);
    else printf("\t\t%d %s   \tlength: %d\n", i, temp_name, temp_len);
    total_records *= temp_len;
    free(temp_name);
  }

我应该如何摆脱这些警告。对结果有害吗?

谢谢,

迈克尔

【问题讨论】:

    标签: g++ gcc-warning netcdf


    【解决方案1】:

    尝试使用 z 修饰符。 size_t 值基本上是 %zu。

    这将是结果:

    printf("\t\t%d %s   \tlength: %zu\n", i, temp_name, temp_len);
    

    看看这个问题:

    How can one print a size_t variable portably using the printf family?

    【讨论】:

    • 你需要%zu,而不是%zd,因为size_t是一个无符号类型。
    • 哇,完全错过了,谢谢你的答案已经修复。
    猜你喜欢
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2012-10-04
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多