2013-04-28 164 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

相關問題