参考:https://blog.csdn.net/jingzi123456789/article/details/66478310
实测:
#include <stdio.h>
int main(int argc, const char * argv[]) {
int a[5] = {1,2,3,4,5};
int *p;
printf(" a = %p\n",a); // 表示数组首元素的地址
printf("a + 1 = %p\n",a + 1); // 表示数组第二个元素的地址
printf("a + 2 = %p\n",a + 2); // 表示数组第三个元素的地址
printf("a + 3 = %p\n",a + 3); // 表示数组第四个元素的地址
printf("a + 4 = %p\n\n",a + 4); // 表示数组第五个元素的地址
printf(" *a = %d\n",*a); // 数组第一个元素的值
printf("*(a + 0) = %d\n",*(a + 0)); // 数组第一个元素的值
printf("*(a + 1) = %d\n",*(a + 1)); // 数组第二个元素的值
printf("*(a + 2) = %d\n",*(a + 2)); // 数组第三个元素的值
printf("*(a + 3) = %d\n",*(a + 3)); // 数组第四个元素的值
printf("*(a + 4) = %d\n",*(a + 4)); // 数组第五个元素的值
printf("*(a + 5) = %d\n\n",*(a + 5)); // 未初始化的值
printf("&a[0] = %p\n",&a[0]); // 表示数组首元素的地址
printf("&a[1] = %p\n",&a[1]); // 表示数组第二个元素的地址
printf("&a[2] = %p\n",&a[2]); // 表示数组第三个元素的地址
printf("&a[3] = %p\n",&a[3]); // 表示数组第四个元素的地址
printf("&a[4] = %p\n\n",&a[4]); // 表示数组第五个元素的地址
printf("a[0] = %d\n",a[0]); // 数组第一个元素的值
printf("a[1] = %d\n",a[1]); // 数组第二个元素的值
printf("a[2] = %d\n",a[2]); // 数组第三个元素的值
printf("a[3] = %d\n",a[3]); // 数组第四个元素的值
printf("a[4] = %d\n\n",a[4]); // 数组第五个元素的值
p = a;
printf("p = %p\n",p);
p = &a[0];
printf("p = %p\n",p);
printf("\n");
printf("&p = %p\n",&p);
printf("&a = %p\n",&a);
printf("&a + 1 = %p\n",&a + 1);
printf("&a[0] + 1 = %p\n\n",&a[0] + 1); // 表示数组第二个元素的地址
printf("&a[0] = %p\n",&a[0]);
printf("sizeof(a) = %lu\n",sizeof(a));
printf("sizeof(&a) = %lu\n",sizeof(&a));
if (a == &a[0]) {
printf("表示的为同一个内存地址\n");
}
return 0;
}
打印结果:
a = 0x7ffeefbff520
a + 1 = 0x7ffeefbff524
a + 2 = 0x7ffeefbff528
a + 3 = 0x7ffeefbff52c
a + 4 = 0x7ffeefbff530
*a = 1
*(a + 0) = 1
*(a + 1) = 2
*(a + 2) = 3
*(a + 3) = 4
*(a + 4) = 5
*(a + 5) = 32766
&a[0] = 0x7ffeefbff520
&a[1] = 0x7ffeefbff524
&a[2] = 0x7ffeefbff528
&a[3] = 0x7ffeefbff52c
&a[4] = 0x7ffeefbff530
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
p = 0x7ffeefbff520
p = 0x7ffeefbff520
&p = 0x7ffeefbff508
&a = 0x7ffeefbff520
&a + 1 = 0x7ffeefbff534
&a[0] + 1 = 0x7ffeefbff524
&a[0] = 0x7ffeefbff520
sizeof(a) = 20
sizeof(&a) = 8
表示的为同一个内存地址
Program ended with exit code: 0
网友评论