Declaring an array without the highest dimension

what will be the dimension of the integer arr if declared as follows:

int arr[][2]={1,2,3}; 

What does it mean to use a non-declaration of the highest dimension?

+3
source share
2 answers

Most likely my expression:

 int arr[][2]={1,2,3}; 

appears to be legal. You can legally omit internal curly braces in initializers. For example, if you want to explicitly define the boundaries of arr and initialize all its elements, you can write either:

 int arr[2][2] = { { 1, 2}, { 3, 4 } }; 

or, equivalently:

 int arr[2][2] = { 1, 2, 3, 4 }; 

You can also lower the engaging elements and they will be implicitly set to zero. For a one-dimensional array, this is straightforward; this is:

 int arr[4] = { 1, 2, 3 }; 

is equivalent to:

 int arr[4] = { 1, 2, 3, 0 }; 

And if you omit the measurement, this can be done from the initializer:

 int arr[] = { 1, 2, 3, 4 }; // 4 elements 

Since you define arr as an array of 2 element int arrays, this is:

 int arr[][2]={1,2,3}; 

equivalent to this:

 int arr[][2] = { { 1, 2 }, { 3 } }; 

There are two elements in the initializer (each of which initializes a 2-element array), which is equivalent to:

 int arr[2][2] = { { 1, 2 }, { 3 } }; 

Finally, you missed the last subitem of the initializer, so it is implicitly zero:

 int arr[2][2] = { { 1, 2 }, { 3, 0 } }; 

In general, omitting dimensions and finalizing initializers is useful because it allows the compiler to figure out some things. If I write:

 char message[] = "hello, world"; 

I don't need to count the characters in the string (and add 1 for the trailing '\0' ). Computers are really good at counting things; having made a man, this work will be stupid.

Similarly, omitting some initializers allows you to provide only the necessary information. With the addition of C99 designated initializers, you can even initialize a specific member of the structure and let the compiler take care of the rest:

 struct foo obj = { .something = 42 }; 

Regarding the alignment of initializers, omitting the internal curly braces, I personally do not see much sense for this, except in the special case of using the initializer { 0 } for initialization and a full array or structure to zero. In particular, for multidimensional arrays, I find it much clearer to show the entire structure of the array.

+5
source

For the minimum bracketing rule (6.7.9p20 in C11),

a sufficient number of initializers from the list is taken into account for elements or members of a sub-aggregate or the first member of a joint union; any remaining initializers are left to initialize the next element or member of the population, of which the current sub-aggregate or containing the union is a part.

So the following declarations are equivalent:

 int arr[][2]={1,2,3}; int arr[][2]={{1,2},{3}}; int arr[2][2]={{1,2},{3}}; 

How it is useful: when initializing a large multidimensional array for the reader, it may be obvious how many objects are in each summation; in this case, there is no need to supply brackets between sub-units.

+1
source

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


All Articles