Is pointer just an integer?

c, c++, integer, pointers

Solution

It's not guaranteed by the standard that such cast would always work.

To store a pointer in an integral type, use `intptr_t` (or its unsigned cousin `uintptr_t`). It's guaranteed to convert `void *` pointers to such types and convert back, resulting the same value.

Note that these types are optional.

Problem

If I know the address of an data object, could I store the address as an integer and operate the integer as a pointer? For example, ``` void main(){ long a = 101010; long *p = &a; long b = p; printf("%lld\n", *(long*)b); } ``` Is it always safe? Comments: `long b = p;` produces a warning: ``` Initialization makes integer from pointer without a cast ``` However, the program prints `101010`.

Original source

Related problems