The Wayback Machine - https://web.archive.org/web/20240811203413/https://www.geeksforgeeks.org/c-pointer-to-pointer-double-pointer/
Open In App

C – Pointer to Pointer (Double Pointer)

Last Updated : 08 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite: Pointers in C

The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to change the values of normal pointers or create a variable-sized 2-D array. A double pointer occupies the same amount of space in the memory stack as a normal pointer.

double pointer in c

 

Declaration of Pointer to a Pointer in C

Declaring Pointer to Pointer is similar to declaring a pointer in C. The difference is we have to place an additional ‘*’ before the name of the pointer.

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.

The above diagram shows the memory representation of a pointer to a pointer. The first pointer ptr1 stores the address of the variable and the second pointer ptr2 stores the address of the first pointer.  

Example of Double Pointer in C

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?

how double pointer works in C

The working of the double-pointer can be explained using the above image:

  • The double pointer is declared using the syntax shown above.
  • After that, we store the address of another pointer as the value of this new double pointer.
  • Now, if we want to manipulate or dereference to any of its levels, we have to use Asterisk ( * ) operator the number of times down the level we want to go.

Size of Pointer to Pointer in C

In the C programming language, a double pointer behaves similarly to a normal pointer in C. So, the size of the double-pointer variable is always equal to the normal pointers. We can verify this using the below C Program.

Example 1: C Program to find the size of a pointer to a pointer.

C




// 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 

Note: The output of the above code also depends on the type of machine which is being used. The size of a pointer is not fixed in the C programming language and it depends on other factors like CPU architecture and OS used. Usually, for a 64-bit Operating System, the size will be 8 bytes and for a 32-bit Operating system, the size will be 4 bytes.

Application of Double Pointers in C

Following are the main uses of pointer to pointers in C:

  • They are used in the dynamic memory allocation of multidimensional arrays.
  • They can be used to store multilevel data such as the text document paragraph, sentences, and word semantics.
  • They are used in data structures to directly manipulate the address of the nodes without copying.
  • They can be used as function arguments to manipulate the address stored in the local pointer.

Multilevel Pointers in C

Double Pointers are not the only multilevel pointers supported by the C language. What if we want to change the value of a double pointer?

In this case, we can use a triple pointer, which will be a pointer to a pointer to a pointer i.e, int ***t_ptr.

Syntax of Triple Pointer

pointer_type *** pointer_name;  

Similarly, to change the value of a triple pointer we can use a pointer to a pointer to a pointer to a pointer (Four level Pointer). In other words, we can say that to change the value of a ” level – x ” variable we can use a ” level – x+1 ” pointer. And this concept can be extended further.

Note: We can use any level pointer in C. There is no restriction about it but it makes the program very complex and vulnerable to errors.

Must Read – Function Pointer in C



Previous Article
Next Article

Similar Reads

Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Pointer to an Array | Array Pointer
Prerequisite: Pointers Introduction  Consider the following program:  [GFGTABS] C #include&lt;stdio.h&gt; int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf(&quot;%p\n&quot;, ptr); return 0; } [/GFGTABS]In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer
9 min read
C Float and Double
Float and double are two primitive data types in C programming that are used to store decimal values. They both store floating point numbers but they differ in the level of precision to which they can store the values. In this article, we will study each of them in detail, their memory representation, and the difference between them. Float Float is
5 min read
C++ default constructor | Built-in types for int(), float, double()
A constructor without any arguments or with default values for every argument is treated as the default constructor. C++ allows even built-in types (primitive types) to have default constructors. The function style cast int() is analogous(similar in some way) to casting 0 to the required type.It will be called by the compiler when in need(precisely
2 min read
Difference between float and double in C/C++
To represent floating point numbers, we use float, double, and long double. What's the difference? double has 2x more precision than float. float is a 32-bit IEEE 754 single precision Floating Point Number - 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision. double is a 64-bit IEEE 754 doubl
3 min read
C program to print a string without any quote (single or double) in the program
Print a string without using quotes anywhere in the program using C or C++. Note : should not read input from the console. The idea is to use macro processor in C (Refer point 6 of this article). A token passed to macro can be converted to a string literal by using # before it. C/C++ Code // C program to print a string without // quote in the progr
1 min read
What is the difference between single quoted and double quoted declaration of char array?
In C, when a character array is initialized with a double-quoted string and the array size is not specified, the compiler automatically allocates one extra space for string terminator '\0'. Example The below example demonstrates the initialization of a char array with a double-quoted string without specifying the size. The below program prints 6 as
2 min read
Modulus of two float or double numbers
Given two floating-point numbers, find the remainder. Examples: Input: a = 36.5, b = 5.0 Output: 1.5 Input: a = 9.7, b = 2.3 Output: 0.5 Recommended PracticeModulus of two double numbersTry It!A simple solution is to do repeated subtraction. [GFGTABS] C++ // C++ program to find modulo of floating // point numbers. #include &lt;bits/stdc++.h&gt; usi
8 min read
How to declare a pointer to a function?
While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly. Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C? Huh..it is pretty simple... int *ptrInteger; /*We have put a * operator between int and ptrInt
2 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the &amp; operator array is an alias for &amp;array[0
1 min read
Article Tags :
three90RightbarBannerImg