# Thread: using new to create a two-dimensional array

1. ## using new to create a two-dimensional array

Can you use new to declare a two-dimensional array from a pointer? So far, I have this:

int main()
{
float *a;
a = new float[1][1];
return 0;
}

I get the following error in the line with the new statement:
'=' : cannot convert from 'float (*)[1]' to 'float *'

Can you not declare a two-dimensional array from a pointer?

Thanks

I doubt it works the way you do it. You could make a "pointer to pointers" which is kind of 2D.
Another solution is to make a 1D array which serves as a 2D:

int* Pointer;
Pointer=new int[x*y];

Remember to deallocate the memory before you quit the program:

delete[] Pointer;

Originally posted by Magos
I doubt it works the way you do it. You could make a "pointer to pointers" which is kind of 2D.
Another solution is to make a 1D array which serves as a 2D:

int* Pointer;
Pointer=new int[x*y];

Remember to deallocate the memory before you quit the program:

delete[] Pointer;
Wouldn't that be interpreted as a 1-D array of size (x*y), i.e., x= 2, y = 3, an array Pointer = new int[6]; ?

4. Yes

Quote:
Another solution is to make a 1D array which serves as a 2D

Access it through Pointer[x+y*xsize]. Element x=2, y=4 in a 5x7 array is element 2+4*5=22 in this "simplified" array. You use a 1D array but handle it somewhat like a 2D array...

5. read zens post here for an example on how to do this properly.