views:

97

answers:

3

The Size of pointer depends on the arch of the machine.

So sizeof(int*)=sizeof(int) or sizeof(int*)=sizeof(long int)

I want to have a custom data type which is either int or long int depending on the size of pointer.

I tried to use macro #if, but the condition for macros does not allow sizeof operator.

Also when using if-else, typedef is limited to the scope of if.

if((sizeof(int)==sizeof(int *)){
  typedef int ptrtype;
}
else{
  typedef long int ptrtype;
}
//ptrtype not avialble here

Is there any way to define ptrtype globally?

+3  A: 

Sure, use intptr_t or uintptr_t defined in <stdint.h>.

avakar
Thanks, It worked.
+6  A: 

In C99 you can use intptr_t.

Carl Norum
And likewise for uintptr_t
Tim Post
Thanks, It worked.
+4  A: 

The traditional way to do this used to be to use GNU autoconf (or similar) to set up some defines such as SIZEOF_VOIDPTR, but if you have a sufficiently recent compiler the type intptr_t may well be what you want. (You might need to set some compiler flags to enable C99 mode; it's in <stdint.h>.)

crazyscot
Thanks, It worked.