Multi-dimensional array initialization Multi-dimensional array initialization c c

Multi-dimensional array initialization


You can initialize arrays in both ways, though the usage of curly inner braces is recommended as it improves the readability.

The easiest way to find the value of an element of a multi-dimensional array non-formatted with braces is by splitting the array. For example, your array's dimensions are 2x3x2:

First split the array into 2 sets (2x3x2)

{14,11,13,10,9,6,8,7,1,5,4,2} --> {{14,11,13,10,9,6}, {8,7,1,5,4,2}}

Then split each set into 3 sets (2x3x2)

{{14,11,13,10,9,6},{8,7,1,5,4,2}} --> {{{14,11}, {13,10} ,{9,6}}, {{8,7}, {1,5}, {4,2}}}

Now, as you see there are 2 elements left in every smaller set (2x3x2), so you have formatted your array with braces.

Now it's simpler to find the value of the element with the index of [1][1][0]. This element is the 2nd ([1][1][0]) bigger set's 2nd ([1][1][0]) smaller set's 1st ([1][1][0]) element, so the answer is 1.


That being said, such an exam question shows the lack of professionalism of your teacher, who's more interested in abusing the programming language syntax, rather than teaching fundamental initialization rules.


The correct answer that should yield full score would be: compile code with all warnings enabled and you won't end up writing crappy code like this.

gcc test.c -std=c11 -pedantic-errors -Wall -Wextratest.c: In function 'main':test.c:6:3: warning: missing braces around initializer [-Wmissing-braces]   int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2};   ^

However, I suspect that your teacher is not so much concerned about the code being crap, but is rather looking for the detail in the C language which allows arrays (and structures) to be initialized even though the brace list does not match the structure of what's being initialized.

As far as the C language is concerned, int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2} is completely equivalent to:

// properly written initialization list for a 3D arrayint Multi[2][3][2] = {   {    {14, 11},    {13, 10},    { 9,  6}  },  {    { 8,  7},    { 1,  5},    { 4,  2}  }};

The only rationale for why the first form is allowed, is because it allows you to write stuff like

int Multi[2][3][2] = {0};

which explicitly initializes the first element to 0 and the rest of the elements as if they had static storage duration (0 as well). Meaning all elements will be set to zero.

Writing things like int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2} is abusing the C language. It is very bad practice. Doing so is banned by MISRA-C and so on.

A good teacher would be concerned about teaching you how to enable all compiler warnings and how to properly initialize multi-dimensional arrays, rather than letting you interpret obfuscated nonsense code.


int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2};

memory map of Multi[2][3][2] is->

Multi[0][0][0]=14;

Multi[0][0][1]=11;

Multi[0][1][0]=13;

Multi[0][1][1]=10;

Multi[0][2][0]=9;

Multi[0][2][1]=6;

Multi[1][0][0]=8;

Multi[1][0][1]=7;

Multi[1][1][0]=1;

Multi[1][1][1]=5;

Multi[1][2][0]=42;

Multi[1][2][1]=2;

therefore the value of Multi[1][1][0]=1; this is so simple

and we can also initialize like this

int Multi[2][3][2] = {{{14,11},{13,10},{9,6}},{{8,7},{1,5},{4,2}}};