Initializing Multidimensional Arrays

Today I saw a question in a C ++ exam:

Given an array int Multi[2][3][2] = {14,11,13,10,9,6,8,7,1,5,4,2} , what is the value of Multi[1][1][0] ?

Shouldn't you initialize 3D arrays as follows: {{{},{}},{{},{}},{{},{}}} ? How can I find the value of an element with such indices? This is so confusing.

+43
c ++ c multidimensional-array
May 20 '15 at 13:10
source share
3 answers

You can initialize arrays in both directions. Using curly braces is optional, but it is recommended that they improve readability.

The easiest way to find the value of an element of an unformatted polyhedral array with curly braces is to split the array. For example, the size of your array is 2x3x2:

First divide the array into 2 sets ( 2 x3x2)

 {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 divide each piece into 3 sets (2x 3 x2)

 {{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 can see, there are 2 left in each smaller set (2x3x 2 ), so you formatted your array with curly braces.

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

+60
May 20 '15 at 13:11
source share

The correct answer, which should give a full point, would be: compile the code with all the warnings turned on, and you will not finish writing crappy code like this.

 gcc test.c -std=c11 -pedantic-errors -Wall -Wextra test.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 that the code is crappy, but rather looking for C language details that allow you to initialize arrays (and structures) even if the list of brackets does not match the initialization structure.

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

 // properly written initialization list for a 3D array int 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 that it allows you to write things 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 a static storage duration (0 also). The value of 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 C. It's very bad practice. This is prohibited by MISRA-C etc.

A good teacher will take care of teaching you how to enable all compiler warnings and how to properly initialize multidimensional arrays, rather than letting you interpret confusing, meaningless code.

+23
May 20 '15 at 1:41
source share

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

Multi memory card [2] [3] [2] - β†’

Multichannel [0] [0] [0] = 14;

Multichannel [0] [0] [1] = 11;

Multichannel [0] [1] [0] = 13;

Multichannel [0] [1] [1] = 10;

Multichannel [0] [2] [0] = 9;

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

Multichannel [1] [0] [0] = 8;

Multichannel [1] [0] [1] = 7;

Multichannel [1] [1] [0] = 1;

Multichannel [1] [1] [1] = 5;

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

Multichannel [1] [2] [1] = 2;

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

and we can also initialize this as

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

+1
May 27 '15 at 8:50
source share



All Articles