简体   繁体   English

如何声明指向2D数组的指针的数组

[英]How to declare of array of pointers to 2D arrays

I have a list of 2D arrays: 我有一个二维数组的列表:

float a[][9] = { ... }
float b[][9] = { ... }
float c[][9] = { ... }

I want to have another array of pointers that point to each of the 2D arrays, like this: 我想要另一个指向每个2D数组的指针数组,如下所示:

what_should_be_here?? arr[] = { a, b, c }

How to achieve this? 如何实现呢?

Use typedef to simplify declaration. 使用typedef简化声明。 Each of the element of arr is float (*)[9] . arr的每个元素都是float (*)[9] Say this type is SomeType . 说这种类型是SomeType Then {a,b,c} means you need an array of three elements of type SomeType . 然后{a,b,c}表示您需要一个由SomeType类型的三个元素组成的SomeType

SomeType arr[] = {a,b,c};

Now the question is, what is SomeType ? 现在的问题是, SomeType什么 So here you go: 因此,您在这里:

typedef float (*SomeType)[9]; //SomeType is a typedef of `float (*)[9]`

SomeType arr[] = {a,b,c}; //this will compile fine now!

As I said, use typedef to simplify declaration! 如我所说, 使用typedef简化声明!

I would choose a better name for SomeType : 我会为SomeType选择一个更好的名称:

typedef float (*PointerToArrayOf9Float)[9];

PointerToArrayOf9Float arr[] = {a,b,c}; 

That is a longer name, but then it makes the code readable! 这是一个较长的名称,但随后使代码可读!

Note that without typedef, your code will look like this: 请注意,没有typedef,您的代码将如下所示:

float (*arr[])[9] = {a,b,c};

which is UGLY. 这很丑。 That is why I will repeat: 这就是为什么我会重复:

Use typedef to simplify declaration! 使用typedef简化声明!

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM