An often required data structure is a "dynamic multi-dimensional arrays" (usually 2-D). There are two principal approaches:
Both approaches are illustrated below (with two versions for approach 1) using a dynamic 2-D array example.
This approach simulates the 2-D array using a 1-D array. Two dimensional indexes must therefore be calculated using the identity: (rowIndex*numCols)+colIndex.
#include < stdio.h > #include < stdlib.h > void main(void) { int numRows, numCols; int *arrayPtr; int rowIndex, colIndex; /* Request for array dimensions. */ printf("Enter values for numRows and numCols\n"); scanf("%d",&numRows); scanf("%d",&numCols); /* Create space for array (Malloc returns NULL if no space available). */ arrayPtr = (int *) malloc(numRows*numCols*sizeof(int)); if (arrayPtr == NULL) { printf("Not enough memory\n"); exit(1); } /* Initialise# the array with a sequence of numbers derived from the indexes. */ for(rowIndex=0;rowIndex < numRows;rowIndex++) { for(colIndex=0;colIndex < numCols;colIndex++) { arrayPtr[(rowIndex*numCols)+colIndex] = (rowIndex*numCols)+colIndex; } } /* Output the result. */ for(rowIndex=0;rowIndex < numRows;rowIndex++) { for(colIndex=0;colIndex < numCols;colIndex++) { printf("%d ",arrayPtr[(rowIndex*numCols)+colIndex]); } printf("\n"); } }
The disadvantage of the above approach is that it is not a real 2-D array in the sense that we cannot index into the array using the standard arrayName[rowIndex][colIndex] approach used with constrained 2-D arrays. One way round this is to use a #define statement to define INDEX(X,Y) as (X*numRows)+Y so that we can now index in using statements of the form arrayName[INDEX(rowIndex,colIndex)] --- not quite what we would desire (some practitioners might even consider it to be a "hack") but close!
#include < stdio.h > #include < stdlib.h > #define INDEX(X,Y) (X*numRows)+Y void main(void) { int numRows, numCols; int *arrayPtr; int rowIndex, colIndex; printf("Enter values for numRows and numCols\n"); scanf("%d",&numRows); scanf("%d",&numCols); /* Create space (Malloc returns NULL if no space available). */ arrayPtr = (int *) malloc(numRows*numCols*sizeof(int)); if (arrayPtr == NULL) { printf("Not enough memory\n"); exit(1); } arrayPtr[INDEX(0,0)] = 0; /* Initialise the array with a sequence of numbers derived from the indexes. */ for(rowIndex=0;rowIndex < numRows;rowIndex++) { for(colIndex=0;colIndex < numCols;colIndex++) { arrayPtr[INDEX(rowIndex,colIndex)] = (rowIndex*numCols)+colIndex; } } /* Output the result. */ for(rowIndex=0;rowIndex < numRows;rowIndex++) { for(colIndex=0;colIndex < numCols;colIndex++) { printf("%d ",arrayPtr[INDEX(rowIndex,colIndex)]); } printf("\n"); } }
If we insist on using the standard arrayName[rowIndex][colIndex] 2-D array indexing approach then we need to do something a bit more complicated --- we need to create an array of arrays. To do this we must use "a pointer to a pointer" to the start of the array (i.e. **arrayName), create space for the first array and then create space for each sub array by iterating (looping) through the first array and so on. Thus:
#include < stdio.h > #include < stdlib.h > void main(void) { int numRows, numCols; int **arrayPtr; int rowIndex, colIndex; printf("Enter values for numRows and numCols\n"); scanf("%d",&numRows); scanf("%d",&numCols); /* Create space for first array (array of pointers to each row). */ arrayPtr = (int **) malloc(numRows*sizeof(int)); if (arrayPtr == NULL) { printf("Not enough memory\n"); exit(1); } /* Loop through first array and create space for according to number of columns.*/ for(rowIndex=0;rowIndex < numRows;rowIndex++) { arrayPtr[rowIndex] = malloc(numCols*sizeof(int)); if (arrayPtr[rowIndex] == NULL) { printf("Not enough memory\n"); exit(1); } } /* Initialise the array with a sequence of numbers derived from the indexes . */ for(rowIndex=0;rowIndex < numRows;rowIndex++) { for(colIndex=0;colIndex < numCols;colIndex++) { arrayPtr[rowIndex][colIndex] = (rowIndex*numCols)+colIndex; } } /* Output the result. */ for(rowIndex=0;rowIndex < numRows;rowIndex++) { for(colIndex=0;colIndex < numCols;colIndex++) { printf("%d ",arrayPtr[rowIndex][colIndex]); } printf("\n"); } }
Created by Frans Coenen (with help from Dave Shield). Maintained by Frans Coenen. Last updated 03 July 2001