Difference between

后端 未结 3 1614
囚心锁ツ
囚心锁ツ 2020-12-06 14:58

I want to get a detailed explanation on the difference between using %d and %p type for printing pointer.

Also Why does

3条回答
  •  天涯浪人
    2020-12-06 15:22

    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.

提交回复
热议问题