Navigating Multi-level Pointers in C: A comprehensive guide to Single, Double and Triple Pointers


Double Pointer

The pointer is a variable which stores the memory address of another variable. So, when we define a pointer to 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.
      Syntax :
           Data_type **variable_Name ;
      Example :
           int **q ;
                q => This is double pointer variable and not normal variable

Triple Pointer

A triple-pointer is a pointer that points to a memory location where a double-pointer is being stored. The triple-pointer itself is just one pointer. Ex. int *** is a pointer, that points to the value of a double pointer, which in turn points to the value of a single pointer, which points to the value of an int.
      Syntax :
           Data_type ***variable_Name ;
      Example :
           int ***r ;
                r => This is double pointer variable and not normal variable

Here is an example of a double pointer being used in a program:

  • The program starts by including the standard input-output library, stdio.h , which is used to input and output data.
  • In the main function, an integer variable a is declared and initialized with the value of 10. A single pointer variable p is also declared.
  • The single pointer variable p is then assigned the memory address of the variable a using the address-of operator & . In this way, p points to the memory location where the value of a is stored.
  • The program then uses the printf function to print the value of a , the memory address of a , the value stored in p (which is the memory address of a ) and the memory address of p .
  • The program uses the dereference operator * to print the value stored at the memory address stored in p , which is the value of a
  • Then, a double pointer variable q is declared and set to the memory address of p using the address-of operator & . Then, the program uses the printf function to print the value of p , the memory address of p , the value stored in q (which is the memory address of p ) and the memory address of q .
  • The program uses the double dereference operator ** to print the value stored at the memory address stored in q , which is the value of p
  • Then, a triple pointer variable r is declared and set to the memory address of q using the address-of operator & . Then, the program uses the printf function to print the value of q , the memory address of q , the value stored in r (which is the memory address of q) and the memory address of r .

The program uses the triple dereference operator *** to print the value stored at the memory address stored in r , which is the value of q

This program demonstrates how to declare and initialize a single pointer, a double pointer and a triple pointer, how to set them to the memory address of a variable, how to print the value stored at a memory address and how to dereference a pointer variables.

Source Code

#include<stdio.h>
int main()
{
    int a=10,*p;
    int **q;   // Pointer to Pointer or Double Pointer
    int ***r; //Triple Pointer
    p=&a; //Address of a
 
    printf("\n Value of  A           : %d",a);
    printf("\n Address of  A         : %d",&a);
    printf("\n Value of  P           : %d",p);
    printf("\n Address of  P         : %d",&p);
    printf("\n P Dereferencing       : %d",*p);
    printf("\n------------------------------------");
 
    q=&p;
    printf("\n Value of  P           : %d",p);
    printf("\n Address of  P         : %d",&p);
    printf("\n Value of  q           : %d",q);
    printf("\n Address of  q         : %d",&q);
    printf("\n **Q Dereferencing     : %d",**q);
    printf("\n------------------------------------");
 
    r=&q;
    printf("\n Value of  q           : %d",q);
    printf("\n Address of  q         : %d",&q);
    printf("\n Value of  r           : %d",r);
    printf("\n Address of  r         : %d",&r);
    printf("\n ***r Dereferencing    : %d",***r);
    printf("\n------------------------------------");
 
    return 0;
}
 
To download raw file Click Here

Output

 Value of  A           : 10
 Address of  A         : 6356732
 Value of  P           : 6356732
 Address of  P         : 6356728
 P Dereferencing       : 10
------------------------------------
 Value of  P           : 6356732
 Address of  P         : 6356728
 Value of  q           : 6356728
 Address of  q         : 6356724
 **Q Dereferencing     : 10
------------------------------------
 Value of  q           : 6356728
 Address of  q         : 6356724
 Value of  r           : 6356724
 Address of  r         : 6356720
 ***r Dereferencing    : 10
------------------------------------

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C