There are two related C standard rules:
C99 standard, 6.3.2.3
:
A pointer to void may be converted to or from a pointer to any
incomplete or object type. A pointer to any incomplete or object type
may be converted to a pointer to void and back again; the result shall
compare equal to the original pointer.
And 7.20.1.4
:
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
It means, that the following code is compliant:
int *p = NULL;
void *q = (void*)p;
uintptr_t s = (uintptr_t)q;
But does it really need the two-step cast? Will the compiler perform an implicit intermediate cast if doing something like:
int *p = NULL;
uintptr_t s = (uintptr_t)p;
(Well, it probably will on most compilers, but my question is about standard compliance)
Best Answer
I wouldn't risk it. The standard makes it abundantly clear what is allowed and what is not allowed.
Writing
uintptr_t s = (uintptr_t)(void*)p;
signals to a reader of your code that you know what you're doing.