#include <stdio.h>
int main() {
int arr[5] = {5,15,1,3,4}; //arr is an array of 5 ints
//arrays are just pointers to their first value
printf("the value of arr: %p\n", arr);
//this should be the same as arr, since arr is just a pointer to its first value
printf("the locate of arr[0]: %p\n", &arr[0]);
//arr+1 is just pointer arithmetic. So arr+1 should be a pointer to the second element in arr.
printf("When we dereference the pointer to the second element in arr, we should get that number: %d\n", *(arr+1));
//Since arr is a pointer to the first element in an array, address-of'ing it gives us the address of that pointer in memory.
printf("*(&arr+1): %p\n", *(&arr+1));
//Adding 1 to the address of a pointer just gives us a higher address that points to nothing in particular (some place on the stack)
}
/*
* Output:
* the value of arr: 0x7ffff2681820
* the locate of arr[0]: 0x7ffff2681820
* When we dereference the pointer to the second element in arr, we should get that number: 15
* *(&arr+1): 0x7ffff2681834
*/
编辑:通过添加其他 4 个内存地址的打印,我们可以看到 *(&addr+1) 指向数组中第五个元素之后的位置。
arr+1: 0x7fff38560224
arr+2: 0x7fff38560228
arr+3: 0x7fff3856022c
arr+4: 0x7fff38560230