#include "cstdio"
#include "string"
int main() {
int arr[4] = {0, 1, 2, 3};
printf("%d\n", sizeof(int));
for (int i = 0; i < 4; ++i) {
printf("%d\n", arr[i]);
}
for (int i = 0; i < 4; ++i) {
printf("%p\n", &arr[i]);
}
printf("%d\n", sizeof(arr));
// 数组的名称 就是指向数组第一个元素的指针
printf("%p\n", arr);
printf("%p\n", &arr[0]);
// 这就意味着我们可以使用数组的名称来访问数组
printf("%d\n", *arr);
// 访问后续的数组 我们可以给 arr+i 再来解引用,就可以访问数组中的元素了
printf("%d\n", *(arr+1));
printf("%d\n", *(arr+2));
printf("%d\n", *(arr+3));
// todo 当然,我们也可以在循环中 使用数组名称作为指针来访问数组元素
for (int i = 0; i < 4; ++i) {
printf("use the name of an arr to access element:%d\n", *(arr+i));
}
// 也可以通过这种方式来修改数组中的元素
*(arr) = 10;
*(arr + 1) = 20;
*(arr + 2) = 30;
*(arr + 3) = 40;
for (int i = 0; i < 4; ++i) {
printf("use the name of an arr to access element:%d\n", *(arr+i));
}
/*
* todo
* It is also considered faster and easier to access two-dimensional arrays with pointers.
* And since strings are actually arrays, you can also use pointers to access strings.
* It is also considered faster and easier to access two-dimensional arrays with pointers.
* And since strings are actually arrays, you can also use pointers to access strings.
*/
return 0;
}