指向char [2] [2]数组的10个指针数组

Mas*_*ard 1 c arrays pointers

对于指向[2][2]char数组的指针,我可以编写:char (*p)[2][2]和一个包含指向char:的类型指针的10个元素的数组char* p[10].

你如何编写10个类型指针元素的数组char[2][2]

为什么这个语句有语法错误?

char (*)[2][2] p[10];
Run Code Online (Sandbox Code Playgroud)

vso*_*tco 6

你需要使用

char (*p[10])[2][2];
Run Code Online (Sandbox Code Playgroud)

但你应该真的使用a typedef,因为这些decl.会变得非常复杂:

typedef char (*ptr_arr)[2][2]; // our pointer-to-char[2][2]
ptr_arr p[10]; // now we have a clean syntax, this is an array of pointers to char[2][2]
Run Code Online (Sandbox Code Playgroud)

你也可以using在C++ 11中使用new

using ptr_arr = char(*)[2][2];
ptr_arr p[10];
Run Code Online (Sandbox Code Playgroud)


Bar*_*rry 5

你应该只使用typedef:

typedef char array[2][2];
typedef array *ptr_to_array[10];

ptr_to_array x; // an array of 10 elements of type pointer to char[2][2]
Run Code Online (Sandbox Code Playgroud)

这样你就不会把所有未来的代码读者都绊倒,让他们盯着它看几分钟.

或者,使用C++ 11:

using ptr_to_array = std::array<
                         std::array<std::array<char, 2>, 2>*,
                         10>;
Run Code Online (Sandbox Code Playgroud)