0

I am trying to initialize a 3-d array but for some reason when I compile, it is giving me a lot of warnings

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

Warnings given:

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]')

When I initialize with just a 2-d array by losing a pair of braces, the compiler doesn't give these warnings. Can anyone suggest what I am doing wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
O_O
  • 4,397
  • 18
  • 54
  • 69
  • You dimensions are **obviously** out of order; from outermost to innermost you have 1, 5, 4 initializers, which doesn't match either [5][4][1] or [1][4][5]. – Jim Balter Mar 17 '14 at 03:13

2 Answers2

4

It is complaining because you said that your inner most array is only supposed to have a single element, yet you are giving it many.

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

If you want more inner elements, change your 1 to something else.

Alpaca
  • 687
  • 3
  • 11
3

The dimension 1 is most peculiar; you also have one too many levels of braces:

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

This would at least compile. The fully braced version would include a pair of braces around each number:

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

That's 5 lines, with 4 sub-arrays on each line, and each sub-array contains a single number.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • More likely, I think, the OP wants to declare it as `array[1][5][4]` and use the current initializer. – Jim Balter Mar 17 '14 at 03:15
  • @JimBalter: Why would you want to add a dimension of size 1 to any array, other than to slow down access? If it was an array with three dynamic dimensions, then 1 might appear as a degenerate case (and I'd not complain), but with statically sized arrays, what's the point of always having to write `[0]` for the index corresponding to the `[1]` in the array definition? That said, the original initializer with the braces that I commented out would initialize `int array[1][5][4]` correctly. But I think the `[1]` is just a mistake. – Jonathan Leffler Mar 17 '14 at 03:18
  • The OP might have an algorithm that works on 3D arrays but his test data only has one instance in one of the dimensions. There's no slowing down of access for a constantly dimensioned array. "would initialize int array[1][5][4] correctly" -- I didn't say otherwise. "But I think the [1] is just a mistake" -- perhaps, but the OP explicitly wrote about initializing a 3-d array. – Jim Balter Mar 17 '14 at 06:59