Typedef for storing pointers in C

The size of the 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 that is either int or long int depending on the size of the pointer.

I tried using macroC # if, but the condition for macros does not allow the sizeof operator.

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

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

Is there a way to define ptrtype globally?

+5
source share
3 answers

GNU autoconf ( ) , SIZEOF_VOIDPTR, , intptr_t , , (, C99, <stdint.h>.)

+4

C99 intptr_t.

+7

Of course, use intptr_tor uintptr_tdefined in <stdint.h>.

+3
source

All Articles