Avoid thinking a 2D array corresponds to an x-y coordinate. C++ has only one-dimensional arrays:
is an array of 3 elements where each element is an int.
is an array of 3 elements where each element is an array of 3 int.
So, the first index is the number of elements in the array and the other indexes just describe the elements.
-
int array[9] = {1,2,3,4,5,6,7,8,9};
-
has a memory layout of:
1 2 3 4 5 6 7 8 9
and this array:
[code]
int tictactoe[3][3] = {1,2,3,4,5,6,7,8,9};
has a memory layout of
1 2 3 4 5 6 7 8 9
So, in memory the arrays are identical.
The indexes descirbe how the array is to be viewed. In the case of array[9], the element with the 8 is array[7]. That is, the 8th element (counting from 0). In teh case of tictactoe[3][3], the 8 is tictactoe[2][1]. That is, it is in the 3rd array (which is tictactoe[2]) and it is element 1 in that array giving tictactoe[2][1].
Locating an element in array, like array[7], consists of adding the size of 7 ints to the address of the array: array[7] = address of array + 7 * sizeof(int).
Locating an element in tictactoe, like tictactoe[2][1] consists of adding 2 times the size of an array of 3 int plus the size of 1 int: tictactoe[2][1] = address of tictactoe + 2* sizeof(3* sizeof(int))+ 1*sizeof(int).
In both cases, the starting address of each array has been increased by the size of 7 ints.
So, the "dimensions" are really how you want to view the array in memory. Regardless of your choice of dimensions, the pointer arithmetic must yield the same address for an element or the compiler won't be able to locate elements.