# Multidimensional arrays

• 11-26-2002
Magos
Multidimensional arrays
int MyArray[4][12];

Is that a 12 x (int[4]) array or a 4 x (int[12]) array?
If you type MyArray[1], do you "access" the 5:th or the 13:th element?

I've made some minor tests on this and it seems like that MyArray[X][Y] is the same as (MyArray[Y]) [X], (X nr of int[Y] arrays), but that seems weird to me, that the X and Y change place.

This is really confusing, lol! :confused: :eek:
• 11-26-2002
Magos
This will print out the adresses, and there are 1000 bytes between each. In the second set, the adresses will print out with 10 bytes between each.
Code:

```int main() {   char MyArray[5][100][10];   cout << (int)MyArray[0] << endl;   cout << (int)MyArray[1] << endl;   cout << (int)MyArray[2] << endl;   cout << (int)MyArray[3] << endl;   cout << (int)MyArray[4] << endl;   cout << (int)MyArray[0][0] << endl;   cout << (int)MyArray[0][1] << endl;   cout << (int)MyArray[0][2] << endl;   cout << (int)MyArray[0][3] << endl;   cout << (int)MyArray[0][4] << endl; }```
It seems like

MyArray[X] == &MyArray[X][0][0]

and

MyArray[0][X] == &MyArray[0][X][0]

(The compiler "fills up" with [0] after the array if the dimensions doesn't agree, correct?).
• 11-26-2002
Fordy
It seems the compiler does assume 0 is the other dimention is ommited...but this makes sense as all that dimention acts as is an offset....so no offset presented; none applied!

1240040
1241040
1242040
1243040
1244040

1240040
1240050
1240060
1240070
1240080

The standard says that int Array[5][6] is implemented like an array of 5( int Array[6]).......

A good way to look at multidimentional arrays is how you have to implement them in your own containters.....Scott Meyers discribes the use of proxy classes to make this possible......and this is simply applying how the compiler implements arrays itself internally (unfortunately operator[][] doesnt exist for built in types or classes - its all arrays of arrays)

Code:

```#include <iostream> template<class T,int alpha,int beta> class MyArray{         class MyProxy{                 T m_t[beta];//array of data         public:                                T& operator[](int i){return m_t[i];}         } m_p[alpha];//array of proxies public:                 MyProxy& operator[](int i){return m_p[i];}        }; int main(void){         const int alpha = 4,                           beta = 5;           MyArray<int,alpha,beta> Arr;           for(int i = 0; i < alpha;++i)                   for(int j = 0; j < beta;++j)                         Arr[i][j] = i + j;         for(int i = 0; i < alpha;++i)                   for(int j = 0; j < beta;++j)                         std::cout << Arr[i][j] << std::endl; }```
• 11-26-2002
Sebastiani
Hmm, nice illustration Fordy. Something interesting to toy with...