2013-04-28 2 views
4

Я использовал некоторые аналогичные сообщения, чтобы изменить код, однако предупреждения все еще сгенерированы.warning: format '% 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

Попробуйте использовать модификатор г. В основном% 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' является неподписанным типом. –

+0

wow полностью пропустил это, спасибо, ответ был исправлен. – Nomad101

+0

Спасибо. Я узнал от вас. –

Смежные вопросы