在C中使用%p [英] The usage of %p in C
问题描述
嗨:
我想知道如何在程序中使用%p。帮助我!
这是否意味着%x?
Hi :
I want to know how to use %p in the program.Help me!
Does it mean %x?
推荐答案
Tak写道:
Tak wrote:
嗨:
我想要知道如何在程序中使用%p。帮助我!
是否意味着%x?
Hi :
I want to know how to use %p in the program.Help me!
Does it mean %x?
如果你的意思是printf风格格式说明符,%p用于指针
类型,而%x用于整数类型,表示显示整数'的
十六进制值。
这是一个例子:
If you mean the printf-style format specifiers, %p is for pointers
types, while %x is for integers types and means "display the integer''s
hexadecimal value".
Here''s an example:
cat format.c
cat format.c
#include< stdio.h>
int main(无效)
{
int i = 10;
printf(" dec - %d \ n",i);
printf(" hex - %x \ n",i) ;
printf(" addr - %p \ n",& i);
return(0);
}
#include <stdio.h>
int main(void)
{
int i = 10;
printf("dec -%d\n", i);
printf("hex -%x\n", i);
printf("addr -%p\n", &i);
return (0);
}
gcc -Wall -o format format.c&& ./format
gcc -Wall -o format format.c && ./format
dec -10
hex -a
addr -0xbfcc4894
-
Pietro Cerutti
PGP公钥:
http://gahr.ch/pgp
Tak< ka ****** @ gmail .comwrote:
Tak <ka******@gmail.comwrote:
我想知道如何在程序中使用%p。帮助我!
I want to know how to use %p in the program.Help me!
当您想要打印对象指针时,可以使用%p。将指针转换为
void *并将其传递给printf()。像这样:
#include< stdio.h>
int main(无效)
{
int object;
printf("对象的地址是%p。\ n",(void *)& object);
返回0;
}
You use %p when you want to print an object pointer. Cast the pointer to
void * and pass it to printf(). Like this:
#include <stdio.h>
int main(void)
{
int object;
printf("The address of object is %p.\n", (void *)&object);
return 0;
}
这是否意味着%x?
Does it mean %x?
否。它表示%p。它打印一个指针。 _How_指针的打印是依赖于系统的
。像%x是一种可能性。所以(像
%4.4X这样的片段):(像%4.4X这样的偏移)。还有很多其他选择。在所有
可能性中,它将取决于您的平台上通常如何打印指针。
Richard
No. It means %p. It prints a pointer. _How_ that pointer is printed is
system-dependent. Like %x is a possibility. So is (segment like
%4.4X):(offset like %4.4X). So are many other options. It will, in all
likelyhood, depend on how pointers are usually printed on your platform.
Richard
>
Pietro Cerutti< gahr_AT_gahr_DOT_ch_DO_NOT_SPAMsaid:
< snip>
Pietro Cerutti <gahr_AT_gahr_DOT_ch_DO_NOT_SPAMsaid:
<snip>
printf(" addr - %p \ n,& i);
printf("addr -%p\n", &i);
未定义的行为。改为使用:
printf(" addr - %p \ n",(void *)& i);
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
Undefined behaviour. Use this instead:
printf("addr -%p\n", (void *)&i);
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
这篇关于在C中使用%p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!