# help with multidimensional array

• 10-30-2008
keira
help with multidimensional array
if 4 lems are created in a 2 by 2 array, why does the for statement beneath the while statement output index 2 and 3 as being NULL?

Could it be the fact that I flattened the array by accessing it sequentially without the row/column notation? I thought that a multidimensional array was stored in memory sequentially anyways, so the way I am doing it should be fine....

Code:

```        for (int r=0; r <= rows; r++)         {                 for (int        c=0; c <= cols; c++)                 {                         lems[r][c] = new Lem(r, c, (r*c)+c);  // this statement is executed 4 times                         cin >> lems[r][c]->agenda;                         cout << lems[r][c]->agenda << endl;                                                                  }         }         Lem** plems = (Lem**)lems;         for(int i = 0; i < size; i++)         {                 if ( plems[i] == 0 )                         cout << "huh?" << i << endl;  // 2 and 3 are NULL?          }```
• 10-30-2008
Daved
Can you show the declaration of lems, rows and cols?
• 10-30-2008
anon
I'm afraid you've left out important information: what are size, rows and cols, and how is lems declared.

Code:

```for (int r=0; r <= rows; r++) for (int        c=0; c <= cols; c++)```
These lines look like almost sure errors (loops for one more row and column than rows and cols).

Code:

`Lem** plems = (Lem**)lems;`
Also looks a bit suspicious.
• 10-30-2008
krappa
You've probably heard that an array is actually a pointer to the first element of your array.
A multi-dimensional array, is actually an array of arrays (or an array of pointers).

What happens is that your lems array stores pointers sequentially, but you have no guarantee that these pointers will be placed sequentially inside the memory.

What this means is that lems[n] actually returns an array of Lems wich you can access using the [] operator.

The memory should look something like this.

... anything ...
address1: { Lem11 , Lem12 }
... more anything ...
address2: { Lem21 , Lem22 }

So what you get if you ask for lems[2] (or anything above) is what comes after address2 in the memory (what I called "anything") which happens to be just NULL in your case.

Also, and I got it wrong above, lems is actually an array of arrays to pointers to Lems (since you instantiate them with new). So if you consider that an array is actually a pointer, it means that lems is a Lem***, and your cast is wrong.
Anyway, as discussed, you couldn't use plems like that since the data is not sequentially placed in the memory.
• 10-30-2008
nucleon
Since a[i][j][k] is just syntactic sugar for *(*(*(a + i) + j) + k)
it would seem that multidimensional arrays are stored contiguously.
Here's a program analogous to yours but using int instead of Lem.
It seems to work just fine.
Code:

```#include <iostream> using namespace std; int main() {     const int rows = 2, cols = 2;     int* a[rows][cols];     for (int r = 0, i = 1; r < rows; ++r)         for (int c = 0; c < cols; ++c)             a[r][c] = new int(i++);     int** p = (int**) a;     for (int i = 0; i < rows * cols; ++i)     {         cout << i;         if ( p[i] ) cout << " = " << *p[i] << endl;         else        cout << " zero pointer\n";     } }```