TYPE arrayName [ size1 ] [ size2 ]; |
The sizes must be a constant value.
int a[3][3]; // a 3x3 "matrix" double b[4][5]; // 4 rows, 5 columns |
arrayName [ index1 ] [ index2 ] |
int a[3][3]; int i, j; for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) cout << a[i][j]; |
In other words, if you want dynamic multi-dimensional arrays, you have to do it yourself
Storage scheme:
|
|
int *a; int nRow, nCol; int i, j; nRow = 3; nCol = 4; a = new int[nRows * nCols]; // Make a one-dim array for ( i = 0; i < nRows; i++ ) for ( j = 0; j < nCols; j++ ) { a[ i*nCols + j ] = 1000*i + j; // Store "a[i][j]" as a[i*nCols + j] } |