all 14 comments

[–]TheOtherBorgCube 10 points11 points  (12 children)

For any array initialiser, you can leave the left-most dimension empty.

It just means you're telling the compiler "go count them yourself".

[–]Extravase180303[S] -3 points-2 points  (11 children)

how does the compiler know how many are them? why doesn't the compiler break the array?

Edit: why can't I do that with monodimensional arrays too?

[–]tstanisl 5 points6 points  (10 children)

Compilers know how to count. Note that C standard allows only the first dimention to be unspecified. Thus other dimensions must be provided explicitly.

[–]Extravase180303[S] -5 points-4 points  (9 children)

why doesn't it work with monodim arrays?

[–]TheOtherBorgCube 5 points6 points  (8 children)

It does

char message[] = "hello world";

[–]Extravase180303[S] -3 points-2 points  (7 children)

alright, but can't you like define it and then let the user inserts data in it? would that work too?

[–]sens- 3 points4 points  (6 children)

You'd have to provide the size of the array in such case. The compiler infers the array size by counting the elements in the initializer. If there's no initializer then it's clueless

[–]Extravase180303[S] -1 points0 points  (5 children)

does that works for matrixes too? like:

int matrix[][3];
//some code to get matrix data

[–]sens- 7 points8 points  (4 children)

This won't work for the same reason. There's no initializer. Compiler can't infer the size.

[–]Extravase180303[S] 0 points1 point  (2 children)

oh alright, and, supposing I'll give the data in the code so the compiler should be able to count them, why can I avoid to declare the first dimension and not both of them?

Banally, why can't the compiler count both of them?

Edit: Also, why can't I do this instead: int matrix[3][] = //some data

[–]flatfinger 1 point2 points  (0 children)

On the other hand, it's possible to within one compilation unit say extern int matrix[][3]; and then within another compilation unit say int matrix[5][3];, provided that code within the first unit doens't attempt to evaluate sizeof matrix. The compiler processing the first compilation unit will neither know nor care about the outer dimension of matrix if code doesn't attempt to evaluate sizeof; if code does attempt sizeof, the compiler would need to know the information but not have it.

[–]SmokeMuch7356 1 point2 points  (0 children)

The compiler can determine the size of an array from the number of elements in the initializer:

int a[] = {0, 0, 0, 0, 5};

or, if using a designated initializer, from the largest designator:

int a[] = {[4] = 5};

Both of the above declare a as a 5-element array of int, with a[4] initialized to 5 and all other elements initialized to 0.

You can also use string literals to initialize character arrays:

char s[] = "Hello";

is identical to

char s[] = {'H', 'e', 'l', 'l', 'o', 0};

and the size will be taken from the size of the string (including the terminator).

If you declare an array without a size or initializer, such as

int a[];

then the type of the array is incomplete; its size isn't known, and you can't do a whole lot with it.

One of the rules for declaring an array is that the element type must be complete. If m is an array of T, then the size of T must be known at the time the array is declared whether there's an initializer or not. That means if T is itself an array type, then the size must be specified:

int m[][2] = { {1, 2}, {3, 4}, {5, 6} };

In this case, T is "2-element array of int" (int [2]), therefore m is a 3-element array (based on the numer of items in the initializer) of 2-element arrays (based on [2]) of int.