%p和%x有什么区别吗?

独孤九剑贰 2013-11-22 12:51:52
#include <stdlib.h>
#include <stdio.h>
int main()
{
unsigned int *p;
p = (unsigned int *)malloc(100);
if(p)
printf("Memory Allocated at: %x",p);
else
printf("Not Enough Memory!\n");
getchar();
p = (unsigned int *)realloc(p,156);
if(p)
printf("Memory Realloc at: %x",p);
else
printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
在linux下编程有警告。
realloc.c: In function ‘main’:
realloc.c:8:10: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat=]
printf("Memory Allocated at: %x",p);
^
realloc.c:14:10: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat=]
printf("Memory Realloc at: %x",p);
但是用%p后可以解决,我想问下%p和%x有什么区别么?
...全文
2007 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnYidan 2013-11-22
  • 打赏
  • 举报
回复
引用 4 楼 max_min_ 的回复:
%p打印地址的啊! %x十六进制形式打印 完成不同!
%p void *; print as a pointer (implementation-dependent representation).
d4shman 2013-11-22
  • 打赏
  • 举报
回复
引用 4 楼 max_min_ 的回复:
%p打印地址的啊! %x十六进制形式打印 完成不同!
版主解释的到位!谢谢~
mLee79 2013-11-22
  • 打赏
  • 举报
回复
%p 标准没有格式的规定, 打印出来像 0x%x 0X%08X %08X 啥格式的都有...
nadleeh 2013-11-22
  • 打赏
  • 举报
回复
引用 楼主 abc594986308 的回复:
#include <stdlib.h> #include <stdio.h> int main() { unsigned int *p; p = (unsigned int *)malloc(100); if(p) printf("Memory Allocated at: %x",p); else printf("Not Enough Memory!\n"); getchar(); p = (unsigned int *)realloc(p,156); if(p) printf("Memory Realloc at: %x",p); else printf("Not Enough Memory!\n"); free(p); getchar(); return 0; } 在linux下编程有警告。 realloc.c: In function ‘main’: realloc.c:8:10: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat=] printf("Memory Allocated at: %x",p); ^ realloc.c:14:10: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned int *’ [-Wformat=] printf("Memory Realloc at: %x",p); 但是用%p后可以解决,我想问下%p和%x有什么区别么?
没什么区别,结果上,唯一的区别的编译器警告的问题
max_min_ 2013-11-22
  • 打赏
  • 举报
回复
%p打印地址的啊! %x十六进制形式打印 完成不同!
angel_su 2013-11-22
  • 打赏
  • 举报
回复
64位下结果会不一样,老实点用%p吧...
Carl_CCC 2013-11-22
  • 打赏
  • 举报
回复
区别就是 %p 相当于 0x%x
whizer 2013-11-22
  • 打赏
  • 举报
回复
p打印指针,x打印整数. printf("Memory Realloc at: %x",p); 改成下面应该也能消除 printf("Memory Realloc at: 0x%x",(unsigned int)p);

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧