先从一个小程序开始:
int a[]={10,20,30,40,50};
char *p,*q;
p=(char*)(a+1);
q=(char*)a;
printf("p is 0x%x\n", p);
printf("q is 0x%x\n", q);
printf("%d is %d\n", p-q, a[p-q]);
a[p-q]输出多少呢? 50? or 20?
(a+1)-a这个1是相对于int而言的,也就是4byte... p-q是相对于char而言的是1....
所以p-q就是4了,答案就是50了...
同理下面这个聪明的你也就知道答案了:
int a[]={10,20,30,40,50};
short *p,*q;
p=(short*)(a+1);
q=(short*)a;
printf("p is 0x%x\n", p);
printf("q is 0x%x\n", q);
printf("%d is %d\n", p-q, a[p-q]);
阅读(564) | 评论(0) | 转发(0) |