How to resolve: "cast to pointer from integer of different size" warning in C code?

Sandeep Singh picture Sandeep Singh · Aug 3, 2012 · Viewed 80.1k times · Source

I am removing gcc warnings from a legacy code.

Is it possible to suppress the warning "cast to pointer from integer of different size" through typecasting:

example:

some_struct *ptr = func()  // func() returns an integer.

Can someone please guide me how to resolve such gcc warnings?

Answer

Eric Postpischil picture Eric Postpischil · Aug 3, 2012

First, if you can fix func (are allowed to modify its source), then fix it. If its computations can be done with pointers, then do them with pointers and return pointers. Sometimes there are valid reasons to work with addresses as integers (e.g., dealing with alignment issues in special code). In that case, change func to use the uintptr_t type (defined in stdint.h). It is designed for treating pointers as integers when necessary. (There is also intptr_t if signed arithmetic is better for some reason, but I generally find the unsigned uintptr_t to be less troublesome.) Preferably, func should convert the uintptr_t to a pointer when returning it, so the return type of func would be a pointer (to something, perhaps some_struct or void).

If you cannot fix func, then you can use casts to tell the compiler that you intend to do the conversions that are being performed. However, this particular error message is telling you that you are not merely converting an integer to a pointer, but that you are converting an integer of one size (e.g., four bytes) to a pointer of another size (e.g., eight bytes). It is likely this code was originally written for a system where the integer type returned by func had the same size as the pointer type, but you are now compiling on a system where the pointer type is larger or less than that integer size.

In that case, you must ensure that the computation performed by func works in the new architecture. If it is returning only a 32-bit value, will that always hold the correct value? That is, nothing will be lost by the missing high 32 bits? No address that func should calculate ever exceeds the maximum value of the integer type it uses? If func is using signed integer types, consider the sign bit too.

If you have ensured that the value returned by func is correct, then you can use explicit casts, such as: some_struct *ptr = (some_struct *) (intptr_t) func();.