指向二维数组的指针 [英] A pointer to 2d array
问题描述
我有一个关于指向二维数组的指针的问题.如果数组类似于
I have a question about a pointer to 2d array. If an array is something like
int a[2][3];
那么,这是一个指向数组a
的指针吗?
then, is this a pointer to array a
?
int (*p)[3] = a;
如果这是正确的,我想知道 [3]
从 int(*p)[3]
是什么意思?
If this is correct, I am wondering what does [3]
mean from int(*p)[3]
?
推荐答案
与其将 int[2][3]
称为二维数组",您应该将其视为一个数组数组'.它是一个包含两个项目的数组,其中每个项目本身就是一个包含 3 个整数的数组.
Rather than referring to int[2][3]
as a '2d array', you should consider it to be an 'array of arrays'. It is an array with two items in it, where each item is itself an array with 3 ints in it.
int (*p)[3] = a;
您可以使用p
来指向a
中的两个项目中的任何一个.p
指向一个三整数数组——即第一个这样的项目.p+1
将指向第二个三整数数组.要初始化 p
以指向第二个元素,请使用:
You can use p
to point to either of the two items in a
. p
points to a three-int array--namely, the first such item. p+1
would point to the second three-int array. To initialize p
to point to the second element, use:
int (*p)[3] = &(a[1]);
以下是指向两项中第一项的等效方法.
The following are equivalent ways to point to the first of the two items.
int (*p)[3] = a; // as before
int (*p)[3] = &(a[0]);
这篇关于指向二维数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!