is it possible to think of it as a matrix?
for example
Isn't this what we are forming
If we do
and we input the word: "WHAT"
how is that being placed in to the matrix I drew.
To display the word I played around and found I can do this:
cout<< array[0][0]<<array[0][1]<<array[0][2]<<array[0][3]
But I thought the maximum it can go to was array [1][1] ?? So how can those arrays be defined.
I mean the only combinations I thought were possible according to a tutorial I read were
array[0][0]
array[0][1]
array[1][0]
array[1][1]
When the compiler saw this line:
it set aside memory for 2 x 2 = 4 bytes of contiguous memory. Let's say it stored array[0][0] at the following address in memory:
The four characters would be stored at the following addresses:
array[0][0] 0xaa0000
array[0][1] 0xaa0001
array[1][0] 0xaa0002
array[1][1] 0xaa0003
If you are referring to the element array[i][j], the computer figures out which address in memory is being referred to by this mathematical equation:
address = address of array[0][0] + offset
where offset is calculated as:
(i times number of rows) + j
In your case the number of rows = 2, so offset is:
So in the case of array[0][3], i = 0 and j = 3, so:
The address of array[0][3] is thus:
which is the address of array[1][1], so array[1][1] and array[0][3] are equivalent here, so when you displayed array[0][3], you were really displaying what was stored at array[1][1], which is 'T'.
However, even though you can get away with it, it's probably not a great habit to get into and you should probably only reference the elements array[0][0], array[0][1], array[1][0], and array[1][1].