How do I allocate memory for a two-dimensional array?
does not work...Code:double* array; array = new double[10][3];
This is a discussion on allocate mem for a multi-dimensional array? within the C++ Programming forums, part of the General Programming Boards category; How do I allocate memory for a two-dimensional array? Code: double* array; array = new double[10][3]; does not work......
How do I allocate memory for a two-dimensional array?
does not work...Code:double* array; array = new double[10][3];
Bit long winded, but it should work......the plus to this is you can substitute the 10 & the 3 with variables, so the array can be whatever size you wishCode:int main(void) { double** array; //pointer to a pointer int x,y; array = new double*[10];//Set first dimentions for(x = 0;x < 10;x++) array[x] = new double[3];//Set second dimentions for(x = 0;x < 10;x++) for(y = 0;y < 3;y++) array[x][y] = 2.69; //Do stuff with array for(x = 0;x < 10;x++) for(y = 0;y < 3;y++) cout << array[x][y] << endl;//Show results for(x = 0;x < 10;x++) delete [] array[x];//Clean up!! delete [] array;//Clean up!!! return 0; }
I thought
double array[10][3];
auto allocates mem for it?
It does, but as gunne was using the new operator, I assumed he/she wanted the array to be declared on the freestore and not the stack.....Originally posted by blood.angel
I thought
double array[10][3];
auto allocates mem for it?