Why is only the second array dimension important?

arrays, c++

Solution

Because compiler needs to figure out how to access the data from memory. The first dimension is not important because compiler can count the number of items when all other sizes are given.

Examples:

int a1[] = { 1, 2, 3, 4 }

compiler knows to allocate space for 4 integers. Now, with this:

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

compiler cannot decide whether it should be a2[1][6] or a2[2][3] or a2[3][2] or a2[6][1]. Once you tell it the second dimension, it can calculate the first one.

For example, trying to access element a2[1][0] would yield different values depending on the declaration. You could get 2, 3, 4 or even invalid position.

Problem

Why when working with two dimensional arrays only second dimension is important for a compiler? Just can't get my head around that. Thanks

Original source