指向多维数组的指针数组

时间:2021-03-10 12:20:00

i have some bidimensional arrays like:

我有一些二维数组,如:

int shape1[3][5] =  {1,0,0,
             1,0,0,
             1,0,0,
             1,0,0,
             1,0,0};
int shape2[3][5] =  {0,0,0,
             0,0,0,
             0,1,1,
             1,1,0,
             0,1,0};

and so on.

等等。

How can i make an array of pointers to those?

我如何制作一系列指针?

I tried the following, but they don't work (warning: initialization from incompatible pointer type):

我尝试了以下,但它们不起作用(警告:从不兼容的指针类型初始化):

int *shapes[]=  {&shape1,&shape2};

int *shapes[]=  {shape1,shape2};

int **shapes[]= {&shape1,shape2};

Any help?

3 个解决方案

#1


Updated Fixed type. Thanks j_radom_hacker for bringing this to my attention!

更新固定类型。感谢j_radom_hacker将此引起我的注意!

[EDIT: Actually the type here was not correct -- see Robert S. Barnes' answer for the correct type to use.]

[编辑:实际上这里的类型不正确 - 请参阅Robert S. Barnes对正确使用类型的回答。]

Figure out the type of shape1 and shape2 first:

首先找出shape1和shape2的类型:

typedef int (*shape_array_t)[5];

Now use this:

现在用这个:

shape_array_t sat[] = { shape1, shape2 };

#2


I believe I just verified what I wrote was correct. The following works as expected:

我相信我刚刚验证了我写的是正确的。以下按预期工作:

#include <stdio.h>

int main(int argc, char **argv) {

int shape1[5][3] =  {1,0,0,
                 1,0,0,
                 1,0,0,
                 1,0,0,
                 1,0,0};

int shape2[5][3] =  {0,0,0,
                 0,0,0,
                 0,1,1,
                 1,1,0,
                 0,1,0};

typedef int (*shapes_p)[3];
shapes_p shapes[2] = { shape1, shape2 };

shapes[0][1][0] = 5;
shapes[1][1][0] = 5;

printf("shape1[1][0] == %d\n", shape1[1][0]);
printf("shape2[1][0] == %d\n", shape2[1][0]);

}

The thing to remember is that the type of shape1 and shape2 is actually:

要记住的是,shape1和shape2的类型实际上是:

int *shape1[5];

What you have in memory is 3 adjacent arrays of 5 ints each. But the actual type is pointer to array of 5 ints. When you write:

你记忆中有3个相邻的阵列,每个5个整数。但实际类型是指向5个整数的数组。当你写:

shape1[1][2] = 1;

shape1 [1] [2] = 1;

you're telling the compiler to index to the second array of int[5] and then access the 3rd element of that array. What the compiler actually does is pointer arithmetic on the underlying type pointed to, in this case int[5]. You could do the same with the following code:

你告诉编译器索引到第二个int [5]数组,然后访问该数组的第3个元素。编译器实际上做的是指向底层类型的指针算法,在本例中为int [5]。您可以使用以下代码执行相同的操作:

int *p = shapes1[0];
p+7 = 1;  // same as shape1[1][2] = 1;

So if you want an array of pointers to int *[5] then you would do:

所以如果你想要一个指向int * [5]的指针数组,那么你会这样做:

typedef int (*shapes_p)[5];
shapes_p shapes[2];

#3


First of all, the first array bound refers to the outermost array dimension, so you should probably declare shape1 as:

首先,第一个数组绑定是指最外层的数组维度,因此您应该将shape1声明为:

int shape1[5][3] =  {1,0,0,
                     1,0,0,
                     1,0,0,
                     1,0,0,
                     1,0,0};

and similarly for shape2.

并且类似于shape2。

[EDIT: I've changed the type of shapes below to correspond to Robert Barnes' answer -- we don't want the outermost subscript to be included in this type!]

[编辑:我已经改变了下面的形状类型以对应罗伯特巴恩斯的答案 - 我们不希望最外面的下标包含在这种类型中!]

The slightly strange-looking typename you need is:

你需要的略带奇怪的类型名称是:

int (*shapes[])[3] = { shape1, shape2 };

This allows the element at row 4, column 1 of shape2 to be addressed using

这允许使用形状2的第4行第1列的元素进行寻址

shapes[1][3][0]

Breakdown of subexpressions and their C types:

子表达式及其C类型的细分:

shapes            // has type "int (*x[2])[3]" (decays to "(**x)[3]")
shapes[1]         // has type "int (*x)[3]"
shapes[1][3]      // has type "int x[3]" (decays to "int *x")
shapes[1][3][0]   // has type "int x"

(Note that a dummy x has been included in the types above to make them clearer -- in fact this identifier is not part of the type.)

(请注意,上面的类型中包含了一个虚拟x以使它们更清晰 - 实际上这个标识符不是该类型的一部分。)

A rule of thumb for decoding C/C++ types is "starting from the variable name, read right when you can and left when you hit a closing parenthesis." So the decoded typename for shapes is:

解码C / C ++类型的经验法则是“从变量名开始,在可以时读右,在到达右括号时离开。”所以形状的解码型名称是:

An array of pointers to an array of 3 integers.

指向3个整数数组的指针数组。

In general it's much nicer to use typedefs for these complicated types, as dirkgently suggests.

一般来说,对于这些复杂类型使用typedef要好得多,正如dirkgently建议的那样。

#1


Updated Fixed type. Thanks j_radom_hacker for bringing this to my attention!

更新固定类型。感谢j_radom_hacker将此引起我的注意!

[EDIT: Actually the type here was not correct -- see Robert S. Barnes' answer for the correct type to use.]

[编辑:实际上这里的类型不正确 - 请参阅Robert S. Barnes对正确使用类型的回答。]

Figure out the type of shape1 and shape2 first:

首先找出shape1和shape2的类型:

typedef int (*shape_array_t)[5];

Now use this:

现在用这个:

shape_array_t sat[] = { shape1, shape2 };

#2


I believe I just verified what I wrote was correct. The following works as expected:

我相信我刚刚验证了我写的是正确的。以下按预期工作:

#include <stdio.h>

int main(int argc, char **argv) {

int shape1[5][3] =  {1,0,0,
                 1,0,0,
                 1,0,0,
                 1,0,0,
                 1,0,0};

int shape2[5][3] =  {0,0,0,
                 0,0,0,
                 0,1,1,
                 1,1,0,
                 0,1,0};

typedef int (*shapes_p)[3];
shapes_p shapes[2] = { shape1, shape2 };

shapes[0][1][0] = 5;
shapes[1][1][0] = 5;

printf("shape1[1][0] == %d\n", shape1[1][0]);
printf("shape2[1][0] == %d\n", shape2[1][0]);

}

The thing to remember is that the type of shape1 and shape2 is actually:

要记住的是,shape1和shape2的类型实际上是:

int *shape1[5];

What you have in memory is 3 adjacent arrays of 5 ints each. But the actual type is pointer to array of 5 ints. When you write:

你记忆中有3个相邻的阵列,每个5个整数。但实际类型是指向5个整数的数组。当你写:

shape1[1][2] = 1;

shape1 [1] [2] = 1;

you're telling the compiler to index to the second array of int[5] and then access the 3rd element of that array. What the compiler actually does is pointer arithmetic on the underlying type pointed to, in this case int[5]. You could do the same with the following code:

你告诉编译器索引到第二个int [5]数组,然后访问该数组的第3个元素。编译器实际上做的是指向底层类型的指针算法,在本例中为int [5]。您可以使用以下代码执行相同的操作:

int *p = shapes1[0];
p+7 = 1;  // same as shape1[1][2] = 1;

So if you want an array of pointers to int *[5] then you would do:

所以如果你想要一个指向int * [5]的指针数组,那么你会这样做:

typedef int (*shapes_p)[5];
shapes_p shapes[2];

#3


First of all, the first array bound refers to the outermost array dimension, so you should probably declare shape1 as:

首先,第一个数组绑定是指最外层的数组维度,因此您应该将shape1声明为:

int shape1[5][3] =  {1,0,0,
                     1,0,0,
                     1,0,0,
                     1,0,0,
                     1,0,0};

and similarly for shape2.

并且类似于shape2。

[EDIT: I've changed the type of shapes below to correspond to Robert Barnes' answer -- we don't want the outermost subscript to be included in this type!]

[编辑:我已经改变了下面的形状类型以对应罗伯特巴恩斯的答案 - 我们不希望最外面的下标包含在这种类型中!]

The slightly strange-looking typename you need is:

你需要的略带奇怪的类型名称是:

int (*shapes[])[3] = { shape1, shape2 };

This allows the element at row 4, column 1 of shape2 to be addressed using

这允许使用形状2的第4行第1列的元素进行寻址

shapes[1][3][0]

Breakdown of subexpressions and their C types:

子表达式及其C类型的细分:

shapes            // has type "int (*x[2])[3]" (decays to "(**x)[3]")
shapes[1]         // has type "int (*x)[3]"
shapes[1][3]      // has type "int x[3]" (decays to "int *x")
shapes[1][3][0]   // has type "int x"

(Note that a dummy x has been included in the types above to make them clearer -- in fact this identifier is not part of the type.)

(请注意,上面的类型中包含了一个虚拟x以使它们更清晰 - 实际上这个标识符不是该类型的一部分。)

A rule of thumb for decoding C/C++ types is "starting from the variable name, read right when you can and left when you hit a closing parenthesis." So the decoded typename for shapes is:

解码C / C ++类型的经验法则是“从变量名开始,在可以时读右,在到达右括号时离开。”所以形状的解码型名称是:

An array of pointers to an array of 3 integers.

指向3个整数数组的指针数组。

In general it's much nicer to use typedefs for these complicated types, as dirkgently suggests.

一般来说,对于这些复杂类型使用typedef要好得多,正如dirkgently建议的那样。