Double Pointers

A double pointer is simple a pointer that points to another pointer
- This is useful for creating dynamic 2-d arrays
- i.e. 2-d arrays where each array can have a different number of objects

This is done by dynamically allocating memory for an array of pointers, and each pointer in this array, points to a dynamically allocated array of integers (or your desired data type)

int **Matrix = (int **) malloc(sizeof(int*) * num_rows); // Creating the first dimension

for (int i = 0; i < num_rows; i++){
	Matrix[i] = (int *) malloc (sizeof(int) * num_col_per_row[i]);
} // Creating the second dimension

The first line creates a pointer to a dynamic array of pointers. [1]
The pointers in this array are then allocated their own dynamic array of integers


  1. See Pointers#The Definition of an Array ↩︎