指向2-D阵列的指针 [英] POINTER TO 2-D ARRAY
问题描述
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int (*ptr)[4]=a;
ptr和* ptr在这种情况下的含义是什么?为什么ptr和* ptr指向相同的meory地址
what do ptr and *ptr mean in this context? Why ptr and *ptr point to the same meory address
推荐答案
ptr
是一个变量,在这种情况下它声明为指向4个整数数组的指针。
要读取声明,请从变量名称开始(ptr
在这种情况下)并在可能的情况下正确行动:
ptr
is a variable, in this case it is declared as a pointer to an array of 4 integers.
To read the declaration, start with the name of the variable (ptr
in this case) and work out, going right when possible:
int (*ptr)[4]=a;
ptr
"ptr is..."
我们不能正确,因为我们首先需要一个匹配的支架,所以我们往左走
We can''t go right because we need a matching bracket first, so we go left
int (*ptr)[4]=a;
(*ptr)
"ptr is a pointer to..."
现在我们已经关闭了括号,所以我们可以再次右转:
Now we have closed the brackets so we can go right again:
int (*ptr)[4]=a;
(*ptr)[4]
"ptr is a pointer to an array of four...
等于结束声明,所以我们必须去再次离开
The equals ends teh declaration, so we have to go left again
int (*ptr)[4]=a;
int (*ptr)[4]=a;
"ptr is a pointer to an array of integers"
并且其余部分指定了一个合适的值。
所以当你在你的代码中稍后使用 ptr
,你指的是由三个四个整数数组组成的数组的实例。
当你引用 * ptr
在你的代码中你指的是四个整数的一个数组,因为*取消引用指针。
这有意义吗?
And the rest of teh line assigns a suitable value.
So when you use ptr
later in your code, you are referring to the instance of an array of three arrays of four integers.
When you refer to *ptr
in your code you are referring to one of the arrays of four integers, because the "*" dereferences the pointer.
Did that make sense?
这篇关于指向2-D阵列的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!