I usually use %x to display pointers. I suppose that isn't portable to 64-bit systems, but it works fine for 32-bitters.
I'll be interested in seeing what answers there are for portable solutions, since pointer representation isn't exactly portable.
I usually use %x to display pointers. I suppose that isn't portable to 64-bit systems, but it works fine for 32-bitters.
I'll be interested in seeing what answers there are for portable solutions, since pointer representation isn't exactly portable.
It's easy to solve if you cast the pointer to a long type. The problem is this won't work on all platforms as it assumes a pointer can fit inside a long, and that a pointer can be displayed in 16 characters (64 bits). This is however true on most platforms.
so it would become:
printf("%016lx", (unsigned long) ptr);
As your link suggests already, the behaviour is undefined. I don't think there's a portable way of doing this as %p itself depends on the platform you're working on. You could of course just cast the pointer to an int and display it in hex:
printf("0x%016lx", (unsigned long)ptr);
Maybe this will be interesting (from a 32-bit windows machine, using mingw):
rjeq@RJEQXPD /u
$ cat test.c
#include <stdio.h>
int main()
{
char c;
printf("p: %016p\n", &c);
printf("x: %016llx\n", (unsigned long long) (unsigned long) &c);
return 0;
}
rjeq@RJEQXPD /u
$ gcc -Wall -o test test.c
test.c: In function `main':
test.c:7: warning: `0' flag used with `%p' printf format
rjeq@RJEQXPD /u
$ ./test.exe
p: 0022FF77
x: 000000000022ff77
As you can see, the %p version pads with zeros to the size of a pointer, and then spaces to the specified size, whereas using %x and casts (the casts and format specifier are highly unportable) uses only zeros.
#include <inttypes.h>
#include <stdint.h>
printf("%016"PRIxPTR"\n", (uintptr_t)ptr);
but it won't print the pointer in the implementation defined way (says DEAD:BEAF for 8086 segmented mode).
Use:
#include <inttypes.h>
printf("0x%016" PRIXPTR "\n", (uintptr_t) pointer);
Or use another variant of the macros from that header.
Also note that some implementations of printf() print a '0x' in front of the pointer; others do not (and both are correct according to the C standard).