warning: cast from pointer to integer of different size

Trying to compile a MUD (online text game) on Ubuntu 14.04 LTS & gcc 4.4.7 and I keep getting this:

vt100.c: In function 'get_tactical_map':

vt100.c:1741: warning: cast from pointer to integer of different size

vt100.c:1805: warning: cast from pointer to integer of different size

The code on each of the above lines is the same:

*pto++ = 'a' + ((int) fch % 25);

I can link the entire file if needed, just not sure where to upload it.


I'm guessing you're trying to compile 32-bit code written by someone unaware of or unconcerned with portability issues of the C language on a 64-bit system.

The problem is that converting a pointer to an integer that cannot represent its value is undefined behaviour. See C11 6.3.2.3 §6:

Any pointer type may be converted to an integer type. [...] If the result cannot be represented in the integer type, the behavior is undefined.

Portable code would have used a cast to uintptr_t (or size_t in the pre-C99 era) instead of int .

Apparently, the address held by the pointer is used as a source of randomness to get an arbitrary lower-case letter (I think it's unlikely that Mints97 is correct and the author just forgot to dereference the pointer, but without some more code, no one can say for sure).

If you ignore the warning, in principle, the code might actually blow up on architectures that raise signals on integer overflow. The more likely case is a possibly harmless bug (depending on how the result is used): Instead of a lower-case letter, you might get a special character or upper-case letter if (int)fch happens to be negative.


I can't say more without variable declarations, but this error normally occurs when you cast a pointer variable to a non-pointer type (for example, void * to int ). In your code, the problem is most likely in (int) fch : fch is a pointer, and you are trying to convert it to int . If you want to extract the value that fch points to, try using (int) *fch , eg

*pto++ = 'a' + ((int)*fch % 25);
链接地址: http://www.djcxy.com/p/28382.html

上一篇: malloc:从不同大小的整数转换为指针[

下一篇: 警告:从指针转换为不同大小的整数