Multi-dimensional Dynamic Arrays
We can also create multi-dimensional dynamic arrays using malloc
. This section will focus on
creating two-dimensional arrays. You can get more dimensions by adapting the following
process:
- Use
malloc
to create an array of pointers. Each pointer in the array will point to a row in the two-dimensional array.
For example:
//Final array will have 3 rows
int **matrix = malloc(3*sizeof(int*));
- Use
malloc
to allocate space for each row:
int i;
for (i = 0; i < 3; i++) {
//Final array will have 4 columns
matrix [i] = malloc(4*sizeof(int));
}
- Now we can treat the pointer like a traditional two-dimensional array. For example, we could set every element to 0:
int j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
matrix [i][j] = 0;
}
}
- When we are done using a multi-dimensional array, we release the memory in reverse order of how we allocated it. So, first we release each row:
for (i = 0; i < 3; i++) {
free(matrix [i]);
}
And then we release the top-level array of pointers:
free(matrix);