4 ms·
Note that "%x" is not the correct way of displaying the value of a pointer with printf(), since "%x" assumes that it's printing an unsigned int and a pointer ma
by greenyoda 12y ago
Note that "%x" is not the correct way of displaying the value of a pointer with printf(), since "%x" assumes that it's printing an unsigned int and a pointer may be longer than an int (e.g., on a 64-bit architecture a pointer would be 8 bytes and an int would typically be 4 bytes). The correct format to use to print a pointer in hex is "%p", which knows how long a pointer is on the current system.
For more details on printf() formats, see:
https://en.wikipedia.org/wiki/Printf_format_string https://en.wikipedia.org/wiki/Printf_format_string
- unwind 12y agoActually %p can only be used to print values of type void * . Since there is no requirement that other pointer types be converted to void * when calling a varargs function, you need to cast: int x = 4711; printf("x is at %p\n", (void *) &x); Of course the conversion will be a null operation on many modern systems, where all data pointer types are implemented identically, but this is what the language standard requires.
- simias 12y agoInteresting, I thought pointer representation could only be different for function pointers. Has there ever been an architecture where data pointers could have a different representations?
- exDM69 12y ago> Interesting, I thought pointer representation could only be different for function pointers. Yes, this is the case, e.g. in micro controllers, where the program is stored in EEPROM. You can store data in program memory (e.g. constant strings) but you need to access it in a special manner. > Has there ever been an architecture where data pointers could have a different representations? At least conceptually, this was the case in early GPUs, where there were different memories for different access patterns. Memory area for constants, another one for global read only memory, and a third area for thread-specific read-write -memory and a fourth that was shared between groups of threads. Whether they were actually different physical memories and whether they had different address spaces is another issue (they at least partially did, some stuff was on chip, some was external DRAM), but this was how it was conceptually seen by the programmer and the way early GPGPU APIs worked (early versions of CUDA and OpenCL).
- nuxi 12y agoYes, different representations as well as non-zero null pointers. More from comp.lang.c FAQ at http://c-faq.com/null/machexamp.html http://c-faq.com/null/machexamp.html.
- simias 12y agoAh, very interesting, thanks!