Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the warning "cast to pointer from integer of different size" from the following code

Tags:

c

gcc

gcc-warning

The code is:

           Push(size, (POINTER)(GetCar(i) == term_Null()? 0 : 1));

Here is the C code push returns ABC which is

 typedef POINTER  *ABC
 typedef void * POINTER
 ABC size;
 Push(ABC,POINTER);
 XYZ GetCar(int);
 typedef struct xyz *XYZ;
 XYZ term_Null(); 
 long int i;

What is the reason for the particular warning?

like image 522
thetna Avatar asked Apr 18 '11 10:04

thetna


1 Answers

You can use intptr_t to ensure the integer has the same width as pointer. This way, you don't need to discover stuff about your specific platform, and it will work on another platform too (unlike the unsigned long solution).

#include <stdint.h>

Push(size, (POINTER)(intptr_t)(GetCar(i) == term_Null()? 0 : 1));

Taken from the C99 Standard:

7.18.1.4 Integer types capable of holding object pointers

1 The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

intptr_t

like image 149
anatolyg Avatar answered Oct 18 '22 15:10

anatolyg