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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:13:44  来源:igfitidea点击:

Where is `%p` useful with printf?

c++cprintf

提问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 %poption?

根据需要格式化指针是微不足道的%x,那么该%p选项是否有一些很好的用途?

回答by Peter Hosey

They do not do the same thing. The latter printfstatement interprets bas an unsigned int, which is wrong, as bis a pointer.

他们不做同样的事情。后一条printf语句解释b为 an unsigned int,这是错误的,就像b一个指针一样。

Pointers and unsigned ints 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), %xwill only print half of the address. On a Mac (and probably some other systems), that willruin the address; the output will be wrong.

指针和unsigned ints 的大小并不总是相同,因此它们不可互换。当它们的大小不同时(一种越来越常见的情况,因为 64 位 CPU 和操作系统变得越来越普遍),%x只会打印地址的一半。在 Mac(可能还有其他一些系统)上,这破坏地址;输出将是错误的。

Always use %pfor 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 0xprefix, 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 %pdisplaying a 0xprefix. On Visual C++, it does not. Use %#pto 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

xis Unsigned hexadecimal integer ( 32 Bit )

x是无符号十六进制整数(32 位)

pis Pointer address

p是指针地址

See printf on the C++ Reference. Even if both of them would write the same, I would use %pto print a pointer.

请参阅C++ 参考中的 printf。即使他们两个都会写相同的,我也会%p用来打印一个指针。

回答by albttx

When you need to debug, use printf with %poption is really helpful. You see 0x0 when you have a NULL value.

当您需要调试时,使用带有%p选项的printf真的很有帮助。当您有一个 NULL 值时,您会看到 0x0。

回答by codaddict

xis used to print t pointer argument in hexadecimal.

x用于以十六进制打印 t 指针参数。

A typical address when printed using %xwould look like bfffc6e4and the sane address printed using %pwould be 0xbfffc6e4

使用打印时的典型地址%x看起来像这样bfffc6e4,使用打印的正常地址%p将是0xbfffc6e4