/* * Demonstrates creating and using a multidimensional array * Note: Use a vector instead if you can, it's much safer. * * Author: Samuel A. Inverso * * License: * Use this code freely, it is distributed without warranty express or * implied. */ #include int ** createArray( int rows, int cols, int initial = 0 ) { int **result = new int*[rows]; // first create an array of pointers ints for( int i=0; i < rows; ++i ) { result[i] = new int[cols]; // second point every pointer to an array of // ints for( int j = 0 ; j < cols ; ++j ) { result[i][j] = initial; } } return result; } void deleteMultiArray( int** ary, int rows ) { while( rows-- ) { delete [] ary[rows]; } delete[] ary; } int main( int argc, char** argv) { int rows = 10; int cols = 10; int **ary = createArray( rows , cols, 1 ); for( int i= 0; i < rows ; ++i ) { for( int j=0; j < cols ; ++j ) { std::cout << ary[i][j] << " "; } std::cout << "\n"; } deleteMultiArray( ary, rows ); }