Table of Contents
C – Pointer To Pointer (Double Pointer)
A pointer-to-pointer (double pointer) is used to store the address of another pointer. The first pointer stores the address of a variable, and the second pointer stores the address of the first pointer. This makes them useful for changing the values of normal pointers or creating variable-sized 2-D arrays. Importantly, a double pointer occupies the same amount of memory as a regular pointer on the stack.
Declaration of Pointer to a Pointer in C
We know that declaring a pointer will be similar to declaring a double pointer in C. The only difference, we can notice is the adding of * just before the pointer name.
data_type_of_pointer **name_of_variable = & normal_pointer_variable;
int val = 5;
int *ptr = &val; // storing address of val to pointer ptr.
int **d_ptr = &ptr; // pointer to a pointer declared
// which is pointing to an integer.
Therefore, this diagram illustrates the first pointer ptr 1 will store the address of the variable and the second pointer ptr 2 can store the address of the first pointer.
Example of Double Pointer in C
// C program to demonstrate pointer to pointer
#include <stdio.h>
int main()
{
int var = 789;
// pointer for var
int* ptr2;
// double pointer for ptr2
int** ptr1;
// storing address of var in ptr2
ptr2 = &var;
// Storing address of ptr2 in ptr1
ptr1 = &ptr2;
// Displaying value of var using
// both single and double pointers
printf("Value of var = %d\n", var);
printf("Value of var using single pointer = %d\n", *ptr2);
printf("Value of var using double pointer = %d\n", **ptr1);
return 0;
}
Output
Value of var = 789
Value of var using single pointer = 789
Value of var using double pointer = 789
How Double Pointer Works?
Int var =10;
int*ptr =&var;
*ptr=20;
Int**ptr=&ptr;
**ptr=30;
- Declare a double-pointer with the appropriate syntax.
- Store the address of another pointer as the value of the double-pointer.
- To manipulate or dereference to any of its levels, use the asterisk (
*
) operator the number of times needed to reach the desired level.
Size of Pointer to Pointer in C
A double-pointer acts similarly to a normal pointer in C. Also, the size of the pointer is equal to a double pointer.
Example 1: C Program to find the size of a pointer to a pointer
// C program to find the size of pointer to pointer
#include <stdio.h>
int main()
{
// defining single and double pointers
int a = 5;
int* ptr = &a;
int** d_ptr = &ptr;
// size of single pointer
printf(" Size of normal Pointer: %d \n", sizeof(ptr));
// size of double pointer
printf(" Size of Double Pointer: %d \n", sizeof(d_ptr));
return 0;
}
Output
Size of normal Pointer: 8
Size of Double Pointer: 8
The size of a pointer in C is not fixed and can vary based on factors like CPU architecture and the operating system. Typically, for a 64-bit operating system, pointer sizes are 8 bytes, while for a 32-bit operating system, they are 4 bytes. This size variation is important to consider when working with pointers, especially when dealing with memory allocation and ensuring your code is compatible across different platforms and systems.
Application of Double Pointers in C
- Dynamic Memory Allocation: Double-pointers are used to allocate and manage memory for multidimensional arrays, enabling flexible data structures.
- Multilevel Data: They are used to store and manage complex hierarchical data structures, like text documents with paragraphs, sentences, and words.
- Data Structures: In data structures, double-pointers allow direct manipulation of node addresses without copying data, improving efficiency.
- Function Arguments: Double-pointers can be used as function arguments to modify the addresses stored in local pointers, facilitating efficient data manipulation.
Multilevel Pointers in C
- In C, we have multilevel pointers beyond double pointers.
- For changing the value of a double pointer, we can use a triple pointer, such as
int ***t_ptr
. - Syntax:
pointer_type ***pointer_name;
Syntax of Triple Pointer
pointer_type *** pointer_name;
- This concept extends further, allowing us to use “level – x + 1” pointers to change the value of a “level – x” variable, and this can be carried to higher levels as needed.
FAQ- C – Pointer To Pointer (Double Pointer)
Q1. How to return a double pointer array in C?
Ans. Declaring a 2D array like int input[][c_in]
doesn’t make the computer interpret these as pointers. input[x][y]
represents integers directly, not memory addresses. If you’re getting addresses when printing, check your code for any issues. Use printf("%d", input[x][y]);
to print integer values.
Q2. What is the advantage of a double-pointer?
Ans. Double pointers in C are powerful tools for efficiently managing data in memory. They allow you to access and manipulate memory addresses, making them versatile for tasks like data reorganization. A pointer, as the name suggests, points to a specific location in memory, enabling you to work with data effectively.
Q3. What are the two types of pointers in C?
Ans. Those are four types of pointers. Those are :
Null Pointer.
Void Pointer.
Wild Pointer.
Dangling Pointer.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together