C++ 指向二维数组的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8617466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:52:33  来源:igfitidea点击:

A pointer to 2d array

c++arrayspointersmultidimensional-array

提问by user1047092

I have a question about a pointer to 2d array. If an array is something like

我有一个关于指向二维数组的指针的问题。如果数组类似于

int a[2][3];

then, is this a pointer to array a?

那么,这是一个指向数组的指针a吗?

int (*p)[3] = a;

If this is correct, I am wondering what does [3]mean from int(*p)[3]?

如果这是正确的,我想知道[3]from是什么意思int(*p)[3]

采纳答案by Aaron McDaid

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[2][3]作为“2D阵列”,则应该认为它是一个“数组的数组”。它是一个包含两个项目的数组,其中每个项目本身就是一个包含 3 个整数的数组。

int (*p)[3] = a;

You can use pto point to either of the two items in a. ppoints to a three-int array--namely, the first such item. p+1would point to the second three-int array. To initialize pto point to the second element, use:

您可以使用p指向 中的两个项目中的任何一个ap指向一个三整数数组——即第一个这样的项目。p+1将指向第二个三整数数组。要初始化p为指向第二个元素,请使用:

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]);

回答by cpx

int a[2][3];

ais read as an array 2 of array 3 of int which is simply an array of arrays. When you write,

a读取为 int 数组 3 的数组 2,它只是一个数组数组。写的时候,

int (*p)[3] = a;

int (*p)[3] = a;

It declares pas a pointer to the first element which is an array. So, ppoints to the array of 3 ints which is a element of array of arrays.

它声明p为指向数组的第一个元素的指针。因此,p指向 3 个整数的数组,它是数组数组的一个元素。

Consider this example:

考虑这个例子:

        int a[2][3]
+----+----+----+----+----+----+
|    |    |    |    |    |    |
+----+----+----+----+----+----+
\_____________/
       |
       |    
       |
       p    int (*p)[3]

Here, pis your pointer which points to the array of 3 ints which is an element of array of arrays.

在这里,p是指向 3 个整数数组的指针,该数组是数组数组的一个元素。

回答by Rob?

Stricly speaking, no, int (*p)[3] = a;is not a pointer to a. It is a pointer to the first element of a. The first element of ais an array of three ints. pis a pointer to an array of three ints.

严格来说,不,int (*p)[3] = a;不是指向a. 它是指向 的第一个元素的指针a。的第一个元素a是一个包含三个整数的数组。p是一个指向三个整数数组的指针。

A pointer to the array awould be declared thus:

一个指向数组的指针a将被声明为:

int (*q)[2][3] = &a; 

The numeric value of pand qare likely (or maybe even required to be) the same, but they are of different types. This will come into play when you perform arithmetic on por q. p+1points to the second element of array a, while q+1points to the memory just beyond the end of array a.

的数值pq有可能(或者甚至要求是)相同,但它们是不同类型的。当您对p或执行算术运算时,这将发挥作用qp+1指向数组的第二个元素a,而q+1指向数组末尾的内存a

Remember: cdeclis your friend: int a[2][3], int (*q)[2][3].

记住:cdecl是你的朋友:int a[2][3], int (*q)[2][3]

回答by Blagovest Buyukliev

The [3]is a part of the type. In this case pis a pointer to an array of size 3 which holds ints.

[3]是该类型的一部分。在这种情况下p是一个指向大小为 3 的数组的指针,该数组包含整数。

The particular type of an array always includes its size, so that you have the types int *[3]or int *[5], but not just int *[]which has undefined size.

数组的特定类型始终包括其大小,因此您可以拥有类型int *[3]or int *[5],但不仅仅是int *[]具有未定义大小的类型。

int *x[20]; /* type of x is int *[20], not just int *[] */
int y[10][10]; /* type of y is int[10][10], not just int[][] */

回答by sdinesh94

Also note:

另请注意:

int *p[5]        // p is an array of 5 pointers

int (*p)[5]      // p points to an array of 5 ints

int (*(p+5))[10] // p is a pointer to a structure where the structure's 5th element has 10 ints .

回答by reza moradi

you can point to 2d array like 1d array

你可以像一维数组一样指向二维数组

#include <iostream>
int main()
{
   int array[2][2] = {{0,1}, {2,3}}; // array
   int *ptr;
   ptr=(int*)array;
   std::cout << *(ptr)   << '\n';//out 0
   std::cout << *(ptr+1) << '\n';//out 1 
   std::cout << *(ptr+2) << '\n';//out 2
   std::cout << *(ptr+3) << '\n';//out 3
}