#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a[5] = {1,2,3,4,5};
int *ptr = (int *)(&a+1);
printf("%d\n%d\n",*(a+1),*(ptr-1));
printf("%x\n%x\n", a, ptr);
getchar();
return 0;
}
运行结果
2
5
29fe08
29fec1
首先看ptr与a的地址,16进制相差14,即20个字节,正好是数组a的长度,说明a取地址偏移1总的偏移量是数组的大小
*(a+1)是数组首地址偏移1,因为数组是整型数组,所以偏移四个字节,所以是数组中的第二个元素,即2,
*(ptr-1)ptr是整型指针,减少1,相当于移动四个字节