【发布时间】:2018-10-09 01:24:43
【问题描述】:
如果你尝试那段代码
#include<stdio.h> int main() {
// Pointer to an integer
int *p;
// Pointer to an array of 5 integers
int (*ptr)[5];
int arr[] = { 3, 5, 6, 7, 9 };
// Points to 0th element of the arr.
// Points to the whole array arr.
ptr = &arr;
printf("p = %p, address of P = %p\n", p, &p);
return 0; }
你会得到类似p = 0x7fff8e9b4370, P address = 0x7fff8e9b4340
这意味着指针P的地址是一个东西,它里面的数据是另一个
但是如果你用这样的数组指针尝试同样的操作
#include<stdio.h> int main() {
// Pointer to an integer
int *p;
// Pointer to an array of 5 integers
int (*ptr)[5];
int arr[] = { 3, 5, 6, 7, 9 };
// Points to 0th element of the arr.
p = arr;
// Points to the whole array arr.
ptr = &arr;
printf("arr = %p, arr address = %p\n", arr, &arr);
return 0; }
你会得到类似arr = 0x7ffda0a04310, arr address = 0x7ffda0a04310的东西
那么为什么指针数据与内存中的指针地址相同?!当我们取消引用 arr 指针的地址时,我们应该得到数字 3,但据我了解,这是内存中的地址位置 0x7ffda0a04310 有 0x7ffda0a04310 作为数据
那么我错在哪里了?
【问题讨论】:
-
你觉得
int (*ptr)[5]是什么类型的?