在C中取消引用指向二维数组的指针数组?

时间:2022-02-06 07:19:29

I was wondering if it is possible to dereference a pointer to a two dimensional array in C:

我想知道是否有可能在C中取消引用指向二维数组的指针:

int matrix1[2][2] = {{1,2},{3,4}};
int matrix2[2][2] = {{4,5},{6,7}};

I am trying to create an array of pointers, where the first pointer points to matrix1 and the second one to matrix2. I thought of

我正在尝试创建一个指针数组,其中第一个指针指向matrix1,第二个指针指向matrix2。我想到了

int *(pt[2]);
pt[0] = &matrix1

My goal would be to be able to access any element in one of the two array with something like:

我的目标是能够访问两个数组之一中的任何元素,例如:

pt[0][n][m];

Or for the second matrix:

或者对于第二个矩阵:

pt[1][n][m]

But as far as I know this doesn't work. Is the pointer declaration wrong?

但据我所知,这不起作用。指针声明错了吗?

2 个解决方案

#1


0  

The answer provided is correct but lacks explanation. I think it is more helpful if you understand why your approach failed.

提供的答案是正确的,但缺乏解释。我认为如果你理解你的方法失败的原因会更有帮助。

Using array indexes on pointers works by incrementing the pointer using the size of the data type.

在指针上使用数组索引通过使用数据类型的大小递增指针来工作。

For example if we define an array as follows:

例如,如果我们定义一个数组如下:

int a[2] = {1,2};

and assume that the array was created in memory at location x

并假设该数组是在位置x的内存中创建的

a[0]; // points at x
a[1]; //points at x + sizeof(int)

now lets take the example of two dimensional array

现在让我们以二维数组为例

int a[2][2] = {{1,2},{3,4}}

again assume location x

再次假设位置x

a[0]; //points at x
a[1]; //points at x + sizeof(int [2]) because int[2] is the data type
a[0][1]; // points at x + sizeof(int)
a[1][1]; // points at x + sizeof(int[2]) + sizeof(int)

Now lets take your example

现在让我们举个例子

int matrix1[2][2] = {{1,2},{3,4}};
int matrix2[2][2] = {{4,5},{6,7}};

int *pt[2];
pt[0] = &matrix1; //doesn't compile

First, that won't really compile because the type of pt is pointer to int and matrix1 is a pointer to int[2]. However, you can force it by casting like this

首先,这不会真正编译,因为pt的类型是指向int的指针,而matrix1是指向int [2]的指针。但是,您可以通过这样的方式强制它

pt[0] = (int*)&matrix1;

But it still won't work because the following is not valid either

但它仍然无效,因为以下内容也无效

pt[0][n][m]; //fails to compile

This is because pt is an array of pointer to int, so pt[0][n] points at an integer, not an array. As such you cannot use apply an index to it

这是因为pt是一个指向int的指针数组,所以pt [0] [n]指向一个整数,而不是一个数组。因此,您无法使用索引

However, if you define it as follows

但是,如果您将其定义如下

int (*pt[2])[2] = {&matrix1,&matrix2}

pt is then an array of pointers to two dimensional arrays. As such, assuming matrix1 defined at x1 and matrix2 defined at x2,

然后pt是指向二维数组的指针数组。因此,假设matrix1定义为x1,matrix2定义为x2,

pt[1][1][1]; //points to x2 + sizeof(int[2]) + sizeof(int)

#2


3  

You have to declare pt as an array of pointers to array.

您必须将pt声明为指向数组的指针数组。

 int (*pt[2])[2] = { matrix1, matrix2 };

Demo.

#1


0  

The answer provided is correct but lacks explanation. I think it is more helpful if you understand why your approach failed.

提供的答案是正确的,但缺乏解释。我认为如果你理解你的方法失败的原因会更有帮助。

Using array indexes on pointers works by incrementing the pointer using the size of the data type.

在指针上使用数组索引通过使用数据类型的大小递增指针来工作。

For example if we define an array as follows:

例如,如果我们定义一个数组如下:

int a[2] = {1,2};

and assume that the array was created in memory at location x

并假设该数组是在位置x的内存中创建的

a[0]; // points at x
a[1]; //points at x + sizeof(int)

now lets take the example of two dimensional array

现在让我们以二维数组为例

int a[2][2] = {{1,2},{3,4}}

again assume location x

再次假设位置x

a[0]; //points at x
a[1]; //points at x + sizeof(int [2]) because int[2] is the data type
a[0][1]; // points at x + sizeof(int)
a[1][1]; // points at x + sizeof(int[2]) + sizeof(int)

Now lets take your example

现在让我们举个例子

int matrix1[2][2] = {{1,2},{3,4}};
int matrix2[2][2] = {{4,5},{6,7}};

int *pt[2];
pt[0] = &matrix1; //doesn't compile

First, that won't really compile because the type of pt is pointer to int and matrix1 is a pointer to int[2]. However, you can force it by casting like this

首先,这不会真正编译,因为pt的类型是指向int的指针,而matrix1是指向int [2]的指针。但是,您可以通过这样的方式强制它

pt[0] = (int*)&matrix1;

But it still won't work because the following is not valid either

但它仍然无效,因为以下内容也无效

pt[0][n][m]; //fails to compile

This is because pt is an array of pointer to int, so pt[0][n] points at an integer, not an array. As such you cannot use apply an index to it

这是因为pt是一个指向int的指针数组,所以pt [0] [n]指向一个整数,而不是一个数组。因此,您无法使用索引

However, if you define it as follows

但是,如果您将其定义如下

int (*pt[2])[2] = {&matrix1,&matrix2}

pt is then an array of pointers to two dimensional arrays. As such, assuming matrix1 defined at x1 and matrix2 defined at x2,

然后pt是指向二维数组的指针数组。因此,假设matrix1定义为x1,matrix2定义为x2,

pt[1][1][1]; //points to x2 + sizeof(int[2]) + sizeof(int)

#2


3  

You have to declare pt as an array of pointers to array.

您必须将pt声明为指向数组的指针数组。

 int (*pt[2])[2] = { matrix1, matrix2 };

Demo.