如何获得3D矩阵中数组的大小?

时间:2019-07-26 19:19:20

标签: c

考虑以下3D矩阵

data %>% 
   group_by(country, years) %>% 
   summarize(avg=mean(c(var1, var2)))

# A tibble: 10 x 3
# Groups:   country [2]
   country years   avg
   <chr>   <int> <dbl>
 1 A        2001    36
 2 A        2002    37
 3 A        2003    38
 4 A        2004    39
 5 A        2005    40
 6 B        2001    41
 7 B        2002    42
 8 B        2003    43
 9 B        2004    44
10 B        2005    45

使用

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 }
  }
};

我希望结果是16。
但是这种情况下的结果是:int i = sizeof(ShapesArray[0]));

我在这里想念什么?

1 个答案:

答案 0 :(得分:3)

char ShapesArray[2]是两个char的数组。因此,第一个元素的大小为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 ++编译器进行编译。