warning: overflow in conversion from 'long long unsigned int' to 'long int' changes value from ........ to .?

Viewed 84

The above warning is giving from this line:

fseek(fp, -sizeof(usr), SEEK_CUR);

here fp is my file pointer and usr is my struct. I am doing the above for updating struct from a file.. I tried out various ways but couldn't remove warning:
more precise warnings:

warning: overflow in conversion from 'long long unsigned int' to 'long int' changes value from '18446744073709551304' to '-312' [-Woverflow] 
    fseek(fp, -sizeof(usr), SEEK_CUR);

1 Answers

You get this warning because sizeof(usr) has type size_t, which is a 64-bit unsigned type on your target system (presumably Windows), negating this unsigned value 312 produces a huge value 18446744073709551304 which cannot be converted to type long without a loss, this out of range conversion being implementation defined anyway. You can fix this problem by writing:

        fseek(fp, -(long)sizeof(usr), SEEK_CUR);

Testing the return value of fseek() would help catch potential problems. Also make sure the stream is open in binary mode.

Related