如何初始化三维数组

时间:2014-03-17 02:16:02

标签: c arrays multidimensional-array

我正在尝试初始化一个三维数组,但由于某些原因,当我编译时,它给了我很多警告

double array[5][4][1]=
{
  {
    {1,2,3,4},
    {5,6,7,8},
    {9,10,11,12},
    {13,14,15,16},
    {17,18,19,20}
  }
};

警告:

sales.c: In function `main':
sales.c:28: warning: excess elements in array initializer
sales.c:28: warning: (near initialization for `array[0][0]')
sales.c:28: warning: excess elements in array initializer
sales.c:28: warning: (near initialization for `array[0][0]')
sales.c:28: warning: excess elements in array initializer
sales.c:28: warning: (near initialization for `array[0][0]')
sales.c:29: warning: excess elements in array initializer
sales.c:29: warning: (near initialization for `array[0][1]')
sales.c:29: warning: excess elements in array initializer
sales.c:29: warning: (near initialization for `array[0][1]')
sales.c:29: warning: excess elements in array initializer
sales.c:29: warning: (near initialization for `array[0][1]')
sales.c:30: warning: excess elements in array initializer
sales.c:30: warning: (near initialization for `array[0][2]')
sales.c:30: warning: excess elements in array initializer
sales.c:30: warning: (near initialization for `array[0][2]')
sales.c:30: warning: excess elements in array initializer
sales.c:30: warning: (near initialization for `array[0][2]')
sales.c:31: warning: excess elements in array initializer
sales.c:31: warning: (near initialization for `array[0][3]')
sales.c:31: warning: excess elements in array initializer
sales.c:31: warning: (near initialization for `array[0][3]')
sales.c:31: warning: excess elements in array initializer
sales.c:31: warning: (near initialization for `array[0][3]')
sales.c:32: warning: excess elements in array initializer
sales.c:32: warning: (near initialization for `array[0][4]')
sales.c:32: warning: excess elements in array initializer
sales.c:32: warning: (near initialization for `array[0][4]')
sales.c:32: warning: excess elements in array initializer
sales.c:32: warning: (near initialization for `array[0][4]')
sales.c:32: warning: excess elements in array initializer
sales.c:32: warning: (near initialization for `array[0]')

当我通过丢失一对大括号来初始化只有一个二维数组时,编译器不会给出这些警告。任何人都可以建议我做错了吗?

2 个答案:

答案 0 :(得分:4)

这是抱怨,因为你说你最内层的数组只应该有一个单独的元素,但你给它很多。

int a[3][2][1] =                                                        
{                                                                       
    {                                                                   
        {1},                                                            
        {2}                                                             
    },                                                                  
    {                                                                   
        {3},                                                            
        {4}                                                             
    },                                                                  
    {                                                                   
        {5},                                                            
        {6}                                                             
    }                                                                   
}; 

如果您想要更多内部元素,请将1更改为其他内容。

答案 1 :(得分:3)

尺寸1是最奇特的;你也有一个太多级别的大括号​​:

double array[5][4][1]=
{
  // {  These braces mean what follows is meant to initialize array[0], but
  //    there are 5 initializers for the 4 elements in array[0], and there
  //    are 4 initializers for each of the 'size 1' sub-arrays which is why
  //    the compiler complains about too many initializers for array[0][0][0], etc.
    {1,2,3,4},
    {5,6,7,8},
    {9,10,11,12},
    {13,14,15,16},
    {17,18,19,20}
  // }
};

这至少会编译。完全支撑的版本将在每个数字周围包括一对括号:

double array[5][4][1]=
{
    { {  1 }, {  2 }, {  3 }, {  4 }, },
    { {  5 }, {  6 }, {  7 }, {  8 }, },
    { {  9 }, { 10 }, { 11 }, { 12 }, },
    { { 13 }, { 14 }, { 15 }, { 16 }, },
    { { 17 }, { 18 }, { 19 }, { 20 }, },
};

这是5行,每行有4个子数组,每个子数组包含一个数字。