Question

I followed some similar posts to modify the code, however the warnings are still generated.

$ 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’

In that block around line 363 and 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);
  }

What should I get rid of the warnings. It that harmful to the results?

Thanks,

Michael

Was it helpful?

Solution

Try using the z modifier. Basically %zu for the size_t value.

this will be the outcome:

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

Take a look at this question:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top