using namespace std; #include /* Utilities for static and dynamic two-dimensional arrays in C++ Static arrays: Stored as blocks in memory in "row-major" order. That is, given int A[3][3], the data is laid out in memory as: A[0][0], A[0][1], A[0][2], A[1][0], A[1][1], etc ... You need to be aware of this when making pointer calculations. "Static" means that memory for the array is allocated at compile time - you must know the dimensions of your array when declaring them. You can only use constants or macros for constants when stating the sizes of arrays. In a single-dimensional array, the memory location of A[i] is simply A+i. However, when given the 2D coordinates of a cell, such as A[i][j], calculating the memory address entails knowing the size of each row. That is, A[i][j] == *(A + (i*rowsize) + j) Static arrays can be more efficient than dynamically allocated arrays because one can treat a static array as a contiguous block of memory. But there are some issues that you need to be aware of. Consider the following function, which tries to print any 2d array passed to it: void print2d(int A[][], int r, int c) { int i, j; for(i=0;i /* at the top of program */ ... memcpy(B,A,160); // 160 == 4*5*8 == size of array in bytes You can type "man memcpy" on any unix/linux prompt to understand how it works. The first argument is the destination address, the second is the source, and the third argument is the number of bytes to copy over. The total size of the array is multiplied by 8 since there are 8 bytes (64 bits) for each double. Functions like memcpy, however, cannot be used on dynamic 2D arrays, because the arrays are scattered all over memory space. You must write a loop to copy individual values separately. */ ///////////////test program: /* Testing multi-dimensional arrays */ int main() { // statically allocated array: static int A[5][4]; // what's static mean? - not a stack variable PRINTM(A,5,4); cout << endl; // dynamically allocated 2-d array; int **B; int n =4; n++; // B = (int **) malloc(5*sizeof(int*)); B = new int*[n]; for (int i=0;i<5;i++) B[i] = new int[4]; for (int i=0;i<4;i++) B[i][i] = 98; printdy(B,5,4); cout << endl; // B = new int[3][4]; Not valid // memory deallocation: every new needs a delete for (int i=0;i<5;i++) delete(B[i]); delete(B); return 0; }