Pointer To Pointer
Author: jebin2 Published: Updated:This is nothing but storing a pointer inside another pointer variable.
Example:
int a = 10;
int *b = &a;
int **c = &b;
Meaning:
bstores the address ofacstores the address ofb
Now, if we want to get the value of a using c:
chas the address ofb- Dereferencing once →
*cgives the address ofa - Dereferencing again →
**cgives the value ofa
Visual (ASCII Diagram)
+-----+ +-----+ +-----+
| a | ---> | b | ---> | c |
+-----+ +-----+ +-----+
a = 10
b = &a
c = &b
*c = b → address of a
**c = *b → value of a (10)
Why use this?
This is super helpful when working with multi-dimensional arrays.
Example:
int **matrix = malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
matrix[i] = malloc(columns * sizeof(int));
}
Breaking it down:
int **matrix = malloc(rows * sizeof(int*));→ Allocates a 1D array of lengthrows, where each element is a pointer (int*).matrix[i] = malloc(columns * sizeof(int));→ Allocates a 1D array ofcolumnsintegers for each row.
So in the end, you’ve built a 2D array dynamically using pointer-to-pointer.
Visualizing the 2D Array
Let’s say rows = 3 and columns = 4.
matrix
|
v
+--------+ +-----------+-----------+-----------+-----------+
| row[0] | ---> | col[0] | col[1] | col[2] | col[3] |
+--------+ +-----------+-----------+-----------+-----------+
+--------+ +-----------+-----------+-----------+-----------+
| row[1] | ---> | col[0] | col[1] | col[2] | col[3] |
+--------+ +-----------+-----------+-----------+-----------+
+--------+ +-----------+-----------+-----------+-----------+
| row[2] | ---> | col[0] | col[1] | col[2] | col[3] |
+--------+ +-----------+-----------+-----------+-----------+
Here:
matrixis a pointer to an array of row pointers.- Each
matrix[i]points to an array of integers (the columns). - Accessing
matrix[i][j]jumps through both levels of pointers.