Source:
First of all %x writes unsigned int
. There is no guarantee that pointer size = unsigned int
size.
As there is no format specifier which takes uintptr_t
, you need to manualy check your pointer size in program and then select correct format specifier.
Therefore, to comply with the standard, you have to manually use the sizeof
operator on void *
and unsigned int
and compare them. If they are equal, you can use %x
as the format.
Otherwise, you need to take appropriate action, and use a different format.
Of course, if this is just a test project, it won't matter to do a dynamic check at runtime. Just hardcode it to the correct value. Be sure to leave a FIXME
though, so you can come back and add dynamic checking later.
Additionally,
We discussed in the comments about the format specifiers, and you specified that %lx
had correct results. Since it is guarenteed that %lx
can print a value of at least uint32_t
, your void *
must be at least the size of uint32_t
.
Additionally number 2,
Source.
If you aren't happy with the implementation-defined behaviour of %p, then use C99 <inttypes.h>
and uintptr_t
instead:
printf("0x%" PRIXPTR "\n", (uintptr_t)your_pointer);
Please note that the above solution requires C99 to work properly.