Initializing a multidimensional array in C

I am reading the book "C Programming" by Kochen, and I'm confused when explaining the initialization of a multidimensional array

here

In particular, I do not understand the meaning of the following sentence. Please note: in this case, the inner pairs of curly braces are required for forced initialization. Without them, the first two lines and the first two elements of the third line would be initialized instead. I'm not sure what this offer means.

+6
source share
5 answers

With internal brackets, the array looks like this:

10 5 -3 0 0 9 0 0 0 0 32 20 1 0 0 0 0 8 0 0 

So, in each line, the last 2 values ​​are zero (because you have not set a value for them. Without internal brackets, the array will look like this:

 10 5 -3 9 0 0 32 20 1 0 0 8 0 0 0 0 0 0 0 0 

Only the first 12 elements will become the set values, and the rest will be 0.

+4
source

This is due to the fact that the array M[4][5] has 20 elements (4 rows, 5 columns), and the default initialization order is row by row, unless the rows are explicitly specified using internal pairs of curly braces.

This means that if you assign the same 12 values ​​as a simple linear list, without internal pairs of curly braces, then the values ​​are assigned to the first two rows (2 * 5 = 10 elements) plus the first 2 columns of the third row. (The remaining 8 elements of the array that you did not explicitly initialize will be automatically set to 0.)

The C compiler knows that each row has only 5 columns and will automatically transfer the list of numbers to the next line every time a field with five columns is reached. In this way,

 int M[4][5] = {10, 5, -3, 9, 0, 0, 32, 20, 1, 0, 0, 8}; 
Under the concept

understood

 int M[4][5] = { {10, 5, -3, 9, 0}, { 0, 32, 20, 1, 0}, { 0, 8, 0, 0, 0}, { 0, 0, 0, 0, 0} }; 

You can redefine the default order using inner curly braces to split your 12 values ​​into rows to your liking (but naturally no more than 5 columns per row for this definition of an array M ).

For example, when you use inner curly braces to separate the same 12 values ​​into four sets of 3 that your page from the book shows , then these inner curly braces are interpreted to initialize individual lines of the multidimensional array. And the result will initialize the four rows of the array, but only the first three columns of these four rows, setting the remaining columns to zero (two empty zero values ​​at the end of each row).

That is, the C compiler knows that the array M has 5 columns in each row, and therefore it will add the missing columns to each row so that each row has 5 columns, and therefore the array will have a total of 20 values:

 int M[4][5] = { {10, 5, -3}, { 9, 0, 0}, {32, 20, 1}, { 0, 0, 8} }; 
Under the concept

understood

 int M[4][5] = { {10, 5, -3, 0, 0}, { 9, 0, 0, 0, 0}, {32, 20, 1, 0, 0}, { 0, 0, 8, 0, 0} }; 
+12
source

Since all arrays inside themselves behave like 1d arrays, you must specify using parentheses whose rows you initialize exactly.

For instance:

 int a[4][5] = { { 1, 2, 3 }, // those are the elements of the first row. // only the first 3 elements are initialized { 1, 2, 3, 4} // those are the elements of the 2nd row. // only the first 4 element are initialized }; // everything else will be equal to 0 

while

 int a[4][5] = { 1, 2, 3, 1, 2, 3, 4}; // this will initialize first 5 elements // of the first row and then continue // with the 2nd one making the first 2 // elements to be 3 and 4 respectivly // everything else will be equal to 0 
+6
source

This will help to see a concrete example.

A multidimensional array is an array of arrays. (This is not just syntactic sugar for a long one-dimensional array.)

It is legal in the initializer to omit both return elements (which are implicitly initialized to zero) and internal curly braces.

Given:

 int arr[2][2]; 

full initialization might look like this:

 int arr[2][2] = { { 10, 20 }, { 30, 40 } }; 

You can (but IMHO shouldn't) omit the inner braces:

 int arr[2][2] = { 10, 20, 30, 40 }; 

and the compiler will map the initializer elements to arr elements.

If you omit the final elements:

 int arr[2][2] = { { 10, 20 } }; 

then the second line is implicitly initialized to { 0, 0 } .

Or you could write:

 int arr[2][2] = { { 10 }, { 20 } }; 

which assigned the values ​​10 and 20 to the first element of each row, not the first row.

Again it’s hard to say what the author was talking about without seeing an example, but the inner bracket tells the compiler to start a new line, even if the first line is incomplete.

If you supply initializers for all 4 elements (or, more generally, all X * Y elements), inner curly braces are not strictly necessary; the order of the elements is the same.

Personally, it’s much easier for me to include all the inner braces, because they reflect the actual structure that you are initializing.

(So, what is the difference between a one-dimensional array and a two-dimensional array, other than syntactic sugar? Given the above declaration of arr , if it was the same as a one-dimensional array, then arr[0][2] will be the same as arr[1][0] , while the second index overflows into the second row, and in practice this can work in practice, but in fact arr[0][2] has undefined behavior. This has practical consequences, the optimizing compiler can accept that all boundaries are in out of range and generate code that doesn't work correctly if it assumption violated.)

See also this question .

+2
source

Multidimensional arrays in C are just “syntactic sugar” for one-dimensional arrays. When you allocate an 4 x 5 int array, you really allocate space for 20 integers per line in memory. These integers are stored as all elements of the first line, then all elements of the second line, etc.

Without inner curly braces, your initializer is also 1D and indicates that you want to initialize the first 12 of these 20 integers, namely the first two lines and the first two elements of the third line.

+1
source

Source: https://habr.com/ru/post/951432/


All Articles