For the program to be well-defined, the format specifier must match the type of the argument. Therefore you can use %p
but not %d
to print out pointers. (The latter might happen to work on some architectures but is technically undefined behaviour.)
The primary reason you can't freely interchange %d
and %p
is that ints
and pointers don't have to have the same size.
The format in which pointers are printed out is architecture-specific (pointers can have different size or indeed different structure). It is, however, common to transcribe memory addresses in hexadecimal, so this is what %p
usually does.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…