Why do I get “cast from pointer to integer of different size” error?

The reason for the warning is that the compiler suspects you might be trying to round-trip a pointer through int and back. This was common practice before the advent of 64-bit machines and it is not safe or reasonable. Of course here the compiler can clearly see that you’re not doing this, and it would be nice if it were smart enough to avoid the warning in cases like this, but it’s not.

A clean alternative that avoids the warning, and another much nastier issue of wrong result when the converted value is negative, is:

unsigned int offset = (uintptr_t) dst % blkLen;

You’ll need to include stdint.h or inttypes.h to have uintptr_t available.

Leave a Comment