考虑以下 3D 矩阵
char ShapesArray[2] = {
(char[4][4]){
{ 0, 1, 0, 0 }, //I
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 }
},
(char[3][3]){
{ 0, 1, 0 }, //J
{ 0, 1, 0 },
{ 1, 1, 0 }
}
};
通过使用
int i = sizeof(ShapesArray[0]));
我预计结果是 16。
但本例的结果是:1
我在这里缺少什么?
最佳答案
char ShapesArray[2]
是两个 char
的数组s。因此第一个元素的大小为 1。打开 compiler warnings :
<source>: In function 'main':
<source>:4:2: error: initialization of 'char' from 'char (*)[4]' makes integer from pointer without a cast [-Werror=int-conversion]
4 | (char[4][4]) {
| ^
<source>:4:2: note: (near initialization for 'ShapesArray[0]')
<source>:10:2: error: initialization of 'char' from 'char (*)[3]' makes integer from pointer without a cast [-Werror=int-conversion]
10 | (char[3][3]) {
| ^
<source>:10:2: note: (near initialization for 'ShapesArray[1]')
cc1: all warnings being treated as errors
Compiler returned: 1
编译器的意思是您正在初始化 char
与char (*)[4]
和char (*)[3]
这是错误的。它甚至无法使用 C++ 编译器进行编译。
关于c - 如何获取 3D 矩阵中数组的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57225924/