According to the standard, there is no guarantee that a pointer fits in an integer type. In practical, otherwise, on mostly personnal computers, there exists several memory models. You can see pointer and integer types have not always the same size (even on "conventional" computers).
You should rather use the optional types intptr_t
and uintptr_t
, from C99.
C11 (n1570), § 7.20.1.4
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
.
The following type designates an unsigned 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: uintptr_t
.
Here is a small example:
#include <stdio.h>
#include <stdint.h>
int n = 42;
int *p = &n;
intptr_t i = (intptr_t)(void *)p;
int *q = (void *)i;
printf("%d\n", *q);