多维数组的初始化

同事写了一个三维数组初始化的code, 其大体如下

 typedef struct
  6 {
  7     uint8_t n;
  8     uint8_t m;
  9 }mipi_clk_ratio;
 10 
 11 static mipi_clk_ratio mipi_clk[2][2][4] =
 12 {
 13         //CSI
 14     {{8,1}, {4,1},{8,3},{2,1}},  //YUV422
 15     {{12,1}, {6,1},{4,1},{3,1}}, //RGB888
 16         //DSI
 17     {{8,1}, {4,1},{8,3},{2,1}},  //YUV422
 18     {{12,1}, {6,1},{4,1},{3,1}}, //RGB888
 19 };


 

在keil下编译运行都没有问题,但是在gcc编译就出现下面的warnings

test1.c:14:2: warning: excess elements in array initializer [enabled by default]
test1.c:14:2: warning: (near initialization for ‘mipi_clk[0]’) [enabled by default]
test1.c:14:2: warning: excess elements in array initializer [enabled by default]
test1.c:14:2: warning: (near initialization for ‘mipi_clk[0]’) [enabled by default]
test1.c:15:2: warning: excess elements in array initializer [enabled by default]
test1.c:15:2: warning: (near initialization for ‘mipi_clk[1]’) [enabled by default]
test1.c:15:2: warning: excess elements in array initializer [enabled by default]
test1.c:15:2: warning: (near initialization for ‘mipi_clk[1]’) [enabled by default]
test1.c:17:2: warning: excess elements in array initializer [enabled by default]
test1.c:17:2: warning: (near initialization for ‘mipi_clk[2]’) [enabled by default]
test1.c:17:2: warning: excess elements in array initializer [enabled by default]
test1.c:17:2: warning: (near initialization for ‘mipi_clk[2]’) [enabled by default]
test1.c:17:2: warning: excess elements in array initializer [enabled by default]
test1.c:17:2: warning: (near initialization for ‘mipi_clk’) [enabled by default]
test1.c:18:2: warning: excess elements in array initializer [enabled by default]
test1.c:18:2: warning: (near initialization for ‘mipi_clk[2]’) [enabled by default]
test1.c:18:2: warning: excess elements in array initializer [enabled by default]
test1.c:18:2: warning: (near initialization for ‘mipi_clk[2]’) [enabled by default]
test1.c:18:2: warning: excess elements in array initializer [enabled by default]
test1.c:18:2: warning: (near initialization for ‘mipi_clk’) [enabled by default]

仔细分析,应该是gcc编译器不能从初始化的数组数据中分清维数,有歧义。可以添加{}来更清晰定义它,如下所示

typedef struct
  6 {
  7     uint8_t n;
  8     uint8_t m;
  9 }mipi_clk_ratio;
 10 
 11 static mipi_clk_ratio mipi_clk[2][2][4] =
 12 {
 13         //CSI
 14     {{{8,1}, {4,1},{8,3},{2,1}},  //YUV422
 15     {{12,1}, {6,1},{4,1},{3,1}}}, //RGB888
 16         //      //DSI
 17     {{{8,1}, {4,1},{8,3},{2,1}},  //YUV422
 18     {{12,1}, {6,1},{4,1},{3,1}}} //RGB888
 19 };

总结一下,有的编译器对多维数组初始化是按行来进行的,所以要求开发者对初始化数据通过添加{}来清晰定义。 此外,为了代码更好的可读性,最好不要定义三维及以上的数组。

你可能感兴趣的:(嵌入式驱动开发杂记)