How to define pointer to pointer in C language?

CServer Side ProgrammingProgramming

Declaring double pointer is similar to declaring pointer in C programming language. The difference is that only we need to add an additional ‘*’ before the name of pointer.

Syntax

The syntax is given below for declaring double pointers −

int **ptr;

Example

Following is the C program for pointer to pointer −

 Live Demo

#include<stdio.h>
// C program to demonstrate pointer to pointer
int main(){
   int element = 24;
   // pointer for element
   int *ptr2;
   // double pointer for ptr2
   int **ptr1;
   // storing address of value in ptr2
   ptr2 = &element;
   // Storing address of ptr2 in ptr1
   ptr1 = &ptr2;
   // Displaying value of element using
   // both single and double pointers
   printf("element value = %d\n", element );
   printf("element value using single pointer = %d\n", *ptr2 );
   printf("element value using double pointer = %d\n", **ptr1);
   return 0;
}

Output

When the above program is executed, it produces the following result −

element value =24
element value using single pointer = 24
element value using double pointer = 24
raja
Published on 09-Mar-2021 09:34:47
Advertisements