【发布时间】:2013-08-19 03:02:26
【问题描述】:
关于问题Why do I have to specify data type each time in C?和我之前的问题how to read memory bytes one by one in hex(so without any format) with printf()
是否可以为我澄清以下问题?
int32_t a[3]={21,3,1000031};
char* p1=&a[0]; /* char is 1-bye and &a[0] is 0x0004 for example */
printf("p1 in hex=%x\n",*p1); /* 4 bytes starting from word-aligned address p1 */
printf("(p1+3)=%d",(p1+3)); /* 4 bytes starting from a NON word-aligned address?* line 2 printf */
printf("p1+3=%p",p1+3) /* line 3 print*/
%x 和 %d 总是 告诉 printf 使用 int 格式,在我的电脑中是 4 字节?我说的对吗?
(p1+3) 是一个非字对齐的地址 Ox004+3=0x007,那么在这种情况下 printf() 显示了什么?换句话说,第 2 行涉及哪些字节打印?
另外,%p formatter(void *) 是否需要 1 个字节来读取(因为 char)或者因为我们谈论指针并且它们总是占用 4 个字节(一个字)?
总结一下我的问题,%d %x %p,.. 他们是从内存中读取恒定大小(取决于 pc)还是取决于相应参数的大小?
【问题讨论】:
-
printf("%d", sizeof(int));会告诉您int在您的系统中有多少字节。 -
您应该使用
%zu打印size_t,而不是%d。 -
@CarlNorum:很高兴知道:)