c - 如何获取 3D 矩阵中数组的大小?

标签 c

考虑以下 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

编译器的意思是您正在初始化 charchar (*)[4]char (*)[3]这是错误的。它甚至无法使用 C++ 编译器进行编译。

关于c - 如何获取 3D 矩阵中数组的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57225924/

相关文章:

c - 重复的 udp 数据包 : how often it happens?

c++ - 我应该对临时内联变量使用++ 运算符吗?

c - 有没有办法对 c 中整数的所有位进行 AND 运算并返回简单的 1 或 0?

控制多个 child ,处理 sigchild

c - 为什么我的代码在加密第二个单词后插入了 3 个空格?

c - Doxygen 文档可能的参数值

c - C 中 fgetc() 函数的行为

c - noNameFunc1 在这里如何真正工作以及如何向它传递值?

字符设备 : choose the device to read/write

c - 实现循环列表以及如何删除列表中的中间节点