# Thread: Dynamic 2D arrays question

1. ## Dynamic 2D arrays question

Code:
```typedef struct {
int rows;
int cols;
int ** element;
} matrix;```
Is there any difference between using this to get to the int element is pointing at

matrix * resultMatrix = new matrix;

resultMatrix->element[i][j]

and this?

*(*((resultMatrix->element)+i))+j)

Forgive me if this seems basic, cos I just wanna make sure

Pier.

2. No, I dont see difference, and as far as I know, the compiler translate this sentence:
arr[i] to *(arr+i)

So, I think, same thing with 2d arrays.

3. No, there is no real difference, except the notation. The variable element is the start-address of the array. It is allowed to access the array by adding a value to the start-address.

4. There is no problem in accessing your example in that manner, however, I do have to point out that what you are working with is not a dynamic 2D array. A 2D array in C and C++ is guaranteed to occupy adjacent space in memory and is not an array of pointers to arrays. A more precise method of working with a "dynamic multidimensional array" can be simulated by dynamically allocating a single-dimension of size numrows*numcolumns, and accessing it via the element row * numcolumns + column. This is what C an C++ do internally when you create and work with a multidimensional array on the stack, etc. Otherwise, your example is just providing a (paritally) syntacticly similar way of working with data as though it were a multidimensional array, but implementation and functionality are completely different. You can no longer use standard pointer arithmetic on the array as a whole, not to mention the fact that you're fragmenting your heap.

5. Originally posted by Polymorphic OOP
etc. Otherwise, your example is just providing a (paritally) syntacticly similar way of working with data as though it were a multidimensional array, but implementation and functionality are completely different. You can no longer use standard pointer arithmetic on the array as a whole, not to mention the fact that you're fragmenting your heap.
Do you mean this..
Code:
```(resultMatrix->element) = new int*[resultMatrix->rows];  /*dynamic allocation of memory*/
for (i=0; i<(resultMatrix->rows); i++)
*((resultMatrix->element)+i) = new int[resultMatrix->cols];/*end of memory allocation*/```

6. First, that's C++, not C. This is a C board. Second, no, that's not what I meant. The example you gave is what you're doing right now.

This is what I mean (since you're using C++, I'll post it in C++)

typedef struct {
int rows;
int cols;
int* element; // Not a pointer to a pointer
} matrix;

resultMatrix->element = new int[resultMatrix->rows * resultMatrix->columns]; /*dynamic allocation*/

There, no looping or anything. This is what's done internally when you make a multidimensional array anyways. In your example you're just making an array of pointers to arrays which is actually very different.

In order to access the above matrix you'd do

resultMatrix->element[ row * resultMatrix->columns + column ]

(you'd put that in a function usually to make things simpler when accessing the matrix)

Since you're using C++, you can just overload operator() to take 2 parameters -- a row and a column so that you can just simply do

resultMatrix( row, column )

to access it

7. Ok I get what you mean....
But I'm stuck with this way of implementing the structure because it was given to me in my assignment.
So I guess my method will work anyway?
Thanks for sharing the info

Pier.

8. Yup, it should still work, it'll just be less efficient and won't be as much like a true "2D Array".