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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…