brackets in N array initialization?

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Hello Everyone!

I don't understand where to put brackets when initializing multidimensional array in c programming?

I have declared and initialized a 4 dimensional array which stores 24 variable

C:
int main ()
{

  int a[2][3][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };

 return 0;
}
I don't understand how to put brackets for each index. How to determine where to put parentheses
 

BobTPH

Joined Jun 5, 2013
8,813
The first level of array had two elements. So put brackets around each two , like:

{1,2},{3,4} …

Next level has 2 elements also, so put brackets around each two groups.

{{1,2},{3,4}}

Next has three elements, so put brackets around each 3 of the above groups. And finally , out another set of brackets around the two groups of 3.

Bob
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
The first level of array had two elements. So put brackets around each two , like:

{1,2},{3,4} …

Next level has 2 elements also, so put brackets around each two groups.

{{1,2},{3,4}}

Next has three elements, so put brackets around each 3 of the above groups. And finally , out another set of brackets around the two groups of 3.

Bob
when i run the code compiler gives me warning
warning: excess elements in array initializer.

C:
 #include <stdio.h>

int main()
{
  int a [2][3][2][2] = {

{
   { 1, 2 },
   { 3, 4 },
},
{
   { 5, 6 },
   { 7, 8 },
},

{
   { 9, 10},
   {11, 12},
},
{
   {13, 14},
   {15, 16},
},

{
   {17, 18},
   {19, 20},
},

{
   {21, 22},
   {23, 24},

};

return 0;
}
 

click_here

Joined Sep 22, 2020
548
It helps if you look at your declaration with some indentation
Code:
int a [2][3][2][2] = 
{ 
    { 
        { 1, 2 }, 
        { 3, 4 }, 
    }, 
    { 
        { 5, 6 }, 
        { 7, 8 }, 
    }, 
    { 
        { 9, 10}, 
        {11, 12}, 
    }, 
    { 
        {13, 14}, 
        {15, 16}, 
    }, 
    { 
        {17, 18}, 
        {19, 20}, 
    }, 
    { 
        {21, 22}, 
        {23, 24}, 
    };
Also note that you don't want the comma after the last element in a subset (i.e "{3,4}," should be "{3,4}" and the same for 8, 12, 16, 20, 24)
 

click_here

Joined Sep 22, 2020
548
I should also add that you don't have to declare all of the elements.

C99 6.7.8 "Initialization"
21 - If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
For an integer this has the value of 0 assigned to it.

That is also how this little trick works for zeroing an int array:
C:
int banana[BUFSIZ] = {0};
int apple[BUFSIZ][BUFSIZ] = {{0}};
... the first element is explicitly set to 0, and all the other elements are automatically set to 0.
 
Top