Pointer to a pointer in C
Pointer to pointer
Also known as a double pointer, is a type of pointer in C that holds the address of another pointer. Which can then be used to access the value stored at the memory location pointed to by the pointer.
data_type **ptr_ptr;
Here, data_type
is the type of the data that the pointer is pointing to.
Example
#include <stdio.h>
// Function that takes a pointer to an int
void increment(int *num)
{
(*num)++; // dereference the pointer and increment the value
}
int main()
{
int num = 0;
printf("num = %d\n", num); // prints "num = 0"
increment(&num); // pass a pointer to the "num" variable
printf("num = %d\n", num); // prints "num = 1"
return 0;
}
Output:
Explanation
- We declare an integer variable
x
, a pointer to an integerp
, and a pointer to a pointer to an integerq
. - We initialize
p
to point to the address ofx
, andq
to point to the address ofp
. - We then print out the addresses and values of
x
,p
, andq
, and demonstrate how to access the values ofx
using bothp
andq
.
Output:
Address of x = 0x7ffeee811ebc
Value of x = 10
Address of p = 0x7ffeee811ec0
Value of p = 0x7ffeee811ebc
Address of q = 0x7ffeee811ec8
Value of q = 0x7ffeee811ec0
Value at p = 10
Value at q = 10
As we can see, by using the pointer to a pointer, we can access the value of x
indirectly, by first dereferencing q
to get the address of p
, and then dereferencing p
to get the value of x
.
Example - two-dimensional arrays using double pointers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows, cols, i, j;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
// Allocate memory for the rows
int **arr = (int **)malloc(rows * sizeof(int *));
// Allocate memory for the columns of each row
for (i = 0; i < rows; i++)
{
arr[i] = (int *)malloc(cols * sizeof(int));
}
// Populate the array with values
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = i * j;
}
}
// Print the array
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
// Free memory
for (i = 0; i < rows; i++)
{
free(arr[i]);
}
free(arr);
return 0;
}
Output:
Explanation
- We first ask the user to input the number of rows and columns for the array.
- We then allocate memory for the rows using a pointer to a pointer
int **arr
, and then allocate memory for the columns of each row using a loop that runs for the number of rows. - We then populate the array with some values, and finally print out the array.
- After we're done using the array, we free the memory allocated for the rows and the array itself using
free()
.
When to use pointer to pointer
Dynamic allocation of two-dimensional arrays using pointers to pointers.
Implementing linked data structures, such as linked lists, trees, and graphs.
Implementing functions that need to modify the value of a pointer passed as a parameter, for example, when inserting a node into a linked list.
Passing an array of pointers to a function for modification or processing.
Implementation of some sorting algorithms, where swapping elements requires the use of pointer to pointer.