C++ `%p` 对 printf 有什么用处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2369541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is `%p` useful with printf?
提问by Moeb
After all, both these statements do the same thing...
毕竟,这两个语句都做同样的事情......
int a = 10;
int *b = &a;
printf("%p\n",b);
printf("%08X\n",b);
For example (with different addresses):
例如(具有不同的地址):
0012FEE0
0012FEE0
It is trivial to format the pointer as desired with %x
, so is there some good use of the %p
option?
根据需要格式化指针是微不足道的%x
,那么该%p
选项是否有一些很好的用途?
回答by Peter Hosey
They do not do the same thing. The latter printf
statement interprets b
as an unsigned int
, which is wrong, as b
is a pointer.
他们不做同样的事情。后一条printf
语句解释b
为 an unsigned int
,这是错误的,就像b
一个指针一样。
Pointers and unsigned int
s are not always the same size, so these are not interchangeable. When they aren't the same size (an increasingly common case, as 64-bit CPUs and operating systems become more common), %x
will only print half of the address. On a Mac (and probably some other systems), that willruin the address; the output will be wrong.
指针和unsigned int
s 的大小并不总是相同,因此它们不可互换。当它们的大小不同时(一种越来越常见的情况,因为 64 位 CPU 和操作系统变得越来越普遍),%x
只会打印地址的一半。在 Mac(可能还有其他一些系统)上,这会破坏地址;输出将是错误的。
Always use %p
for pointers.
始终%p
用于指针。
回答by unwind
At least on one system that is not very uncommon, they do not print the same:
至少在一个并不少见的系统上,它们不会打印相同的内容:
~/src> uname -m
i686
~/src> gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
[some output snipped]
gcc version 4.1.2 (Gentoo 4.1.2)
~/src> gcc -o printfptr printfptr.c
~/src> ./printfptr
0xbf8ce99c
bf8ce99c
Notice how the pointer version adds a 0x
prefix, for instance. Always use %p since it knows about the size of pointers, and how to best represent them as text.
例如,请注意指针版本如何添加0x
前缀。始终使用 %p,因为它知道指针的大小,以及如何最好地将它们表示为文本。
回答by Mauritius
You cannot depend on %p
displaying a 0x
prefix. On Visual C++, it does not. Use %#p
to be portable.
您不能依赖于%p
显示0x
前缀。在 Visual C++ 上,它没有。使用%#p
便携。
回答by Tronic
The size of the pointer may be something different than that of int
. Also an implementation could produce better than simple hex value representation of the address when you use %p
.
指针的大小可能与 的不同int
。此外,当您使用%p
.
回答by Filip Ekberg
x
is Unsigned hexadecimal integer ( 32 Bit )
x
是无符号十六进制整数(32 位)
p
is Pointer address
p
是指针地址
See printf on the C++ Reference. Even if both of them would write the same, I would use %p
to print a pointer.
请参阅C++ 参考中的 printf。即使他们两个都会写相同的,我也会%p
用来打印一个指针。
回答by albttx
When you need to debug, use printf with %p
option is really helpful. You see 0x0 when you have a NULL value.
当您需要调试时,使用带有%p
选项的printf真的很有帮助。当您有一个 NULL 值时,您会看到 0x0。
回答by codaddict
x
is used to print t pointer argument in hexadecimal.
x
用于以十六进制打印 t 指针参数。
A typical address when printed using %x
would look like bfffc6e4
and the sane address printed using %p
would be 0xbfffc6e4
使用打印时的典型地址%x
看起来像这样bfffc6e4
,使用打印的正常地址%p
将是0xbfffc6e4