jackycflau
jackycflau

Reputation: 1121

2-dimensional array definition

From my understanding, when we define an array, we need to specific its size

explicitly so the compiler will know required memory to be occupied by the array.

Therefore,

Case 1:

int a[][3]; //illegal as we will not know the size of the int array
a[0][1]=2;
a[1][3]=4;

However when I add something like this:

Case 2:

int a[][3]={0}; //the compiler didn't complain at all
a[0][1]=2;
a[1][3]=4;

Why wont the compiler show any error message in this case? The definition in case 2 only tells compiler how many columns are there but the number of rows are still unknown? Shouldn't the compiler act in the same way as case 1?? Any reasons to explain this? Thank you so much

Upvotes: 0

Views: 66

Answers (1)

vishal
vishal

Reputation: 2391

when you define array as

int a[][3]={0};

the compiler assigns the value to the the above declared array as:

int a[1][3]

so it did not complained

similarly if you do like this:

int a[][3]={0,2};

it will take:

int a[2][3]

Upvotes: 5

Related Questions