I'm not sure what I get, how do you sometimes want to return a number from errno-base.h, and sometimes a pointer - how would the receiving function tell you about everyone else? This is equal, and then in Linux GCC,
int is 32 bit wide whether you are on 32 or 64 bit linux- pointers are 64-bit wide on 64-bit architectures and 32 bytes wide 32-bit architectures
long have 32-bit width on 32-bit architectures and 64-bit width on 64-bit architectures.long long always have a width of 64 bits.
therefore, on a 64-bit architecture, casting a pointer to int, you will get a 64-bit value up to a 32-bit value, and you can be sure that you will lose some of the 64-bit information from the pointer - and this is what the compiler warns about, since you point to yourself.
If you want to give a pointer to "anonymous", then your choice should be either long , long long , or void* - the most portable of void* .
Another alternative is to write in the form of an offset, that is, if you have a large memory area where you want to βoverlayβ a 32-bit integer, and then convert it to something like that;
static struct mybigbuffer *globalbuffer; int cast2int(void*x) { return (int)(globalbuffer-(struct mybigbuffer*)x); }
however , that only works if you know that your memory will never exceed 2 ^ 31 globalbuf entries and that your pointers will be sure bounds, etc. - therefore, if you are 100% sure that you know what you are doing, I would not recommend this - stick to long or empty * as safe options.
source share