What robatino said is right, but if you want to dynamically create a 2d array, use something like this -
Code:
int **Allocate_2D_Array( int x, int y )
{
int **Array = new int *[y];
for ( int i=0; i<y; i++ )
{
Array[i] = new int [x];
}
return Array;
}
void Dealocate_2d_array ( int **Array, int y )
{
for ( int i=0; i<y; i++ )
{
delete []Array[i];
}
delete []Array;
}
int main( void )
{
int x, y;
// Get your user defined array dimensions
cout<< "Enter X: ";
cin >> x;
cout<< "Enter Y: ";
cin >> y;
// Allocate memory
int **p_2d_array = Allocate_2D_Array( x, y );
// Fill array with variables
for ( int j=0; j<y; j++ )
{
for ( int i=0; i<x; i++ )
{
p_2d_array[j][i] = i+j;
}
}
// print out array
for ( int j=0; j<y; j++ )
{
for ( int i=0; i<x; i++ )
{
cout<< p_2d_array[j][i] << " ";
}
cout<< '\n';
}
// delete allocated memory
Dealocate_2d_array( p_2d_array, y );
return 0;
}
Pre Edit: STL would be better, I think. There is little to no error checking here, and it would be better if you made functions of both the allocation stage and deallocation stage to make it neater.
Post Edit: Here's some functions. I thought I'd not make a really long post, and just edit over the old one.