2013-04-28 163 views
4

我跟着一些类似的帖子来修改代码,但是警告仍然生成。警告:格式'%d'期望类型'int',但参数4的类型为'size_t'

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

我应该摆脱警告。它对结果有害吗?

感谢,

迈克尔

回答

7

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

这将是结果:

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?

+1

你会想'%zu',不'%zd',但是,作为'size_t'是一个无符号类型。 – 2013-04-28 22:16:04

+0

哇完全错过了,谢谢你的回答已经修复。 – Nomad101 2013-04-28 22:23:12

+0

谢谢。我从你那里学到了。 – 2013-04-28 23:11:45

相关问题