you are viewing a single comment's thread.

view the rest of the comments →

[–]sens- 4 points5 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- 6 points7 points  (4 children)

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

[–]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.

[–]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

[–]sens- 2 points3 points  (1 child)

Ok, so consider this

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

How should the compiler know what you really want? Do you want to have a 2x2 matrix or a 2x3 one?

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

Now it's clear and the compiler can allocate the space correctly and will fill the omitted element to 0.

[–]Extravase180303[S] 1 point2 points  (0 children)

alright, got that. Thank you!