69,381
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
int main(void)
{
int a[5]={1,2,3,4,5};
//假设a的地址为0x80000000,sizeof(int)=4
//假设是little-endian,则0x80000000-0x80000013的数据为:
// addr: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13
// data: 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00
int *ptr1=(int *)(&a+1); //ptr1 = 0x80000000+20=0x80000014
int *ptr2=(int *)((int )a+1); //ptr2 = 0x80000000+1 = 0x80000001
//&ptr1[-1] = 0x80000014-4 = 0x80000010,so ptr1[-1] = *(int *)0x80000010 = 5
//*ptr2 = *(int *)0x80000001 = 0x02000000
printf("%x,%x",ptr1[-1],*ptr2);
return 0;
}
这个解释的好。