Table of Contents
C programming language pointer to pointer
In this tutorial, we will discuss the concept of the pointer to a pointer in C programming language.
Normally, the pointer is a special type of variable which stores the memory address of another variable. Pointer to pointer means one of the pointers contain the address of another pointer.
As shown above, x is a variable. It has the memory address 1004 and 1004 is stored at the ptr 1 (pointer 1). ptr 1 has the memory address 1008 which is stored in ptr 2(pointer 2).
How to declare a pointer in C programming?
int *ptr;
How to declare a pointer to pointer in C programming?
int **ptr;
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=15; //assign int variable x and initialazed value
int *ptr1; //assign a pointer variable ptr1 as int
ptr1=&x; //assign address of x to ptr1
int **ptr2; //assign a pointer to pointer variable ptr2 as int
ptr2=&ptr1; //assign address of ptr1 to ptr2
printf(“x value is: %dn”,x); //display value of x
printf(“address of x is: %un”,&x); //display address of x
printf(“the value of ptr1 is: %un”,ptr1); //display value of ptr1
printf(“the address of ptr1 is: %un”,&ptr1); //display address of ptr1
printf(“the value pointed by the pointer ptr1 is: %un”,*ptr1);
printf(“the value of ptr2 is: %un”,ptr2); //display value of ptr2
printf(“the address of ptr2 is: %un”,&ptr2); //display address of ptr2
printf(“the value of x by using pointer to a pointer is: %dn”,**ptr2);
return 0;
}
At above program,
Lets try to understand the above program
X value is 15;
Address in memory location of x is 2686748
The valur of ptr1(pointer1) is 2686748.
Address in memory location ptr1 (pointer 1) 2686744
The value pointed by ptr1(pointer 1) is 15
The valur of ptr1(pointer1) is 2686744
Address in memory location ptr1 (pointer 1) 2686740
memory location Diagram of above program
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks=99; //assign and initialized marks as integer
int *ptr1; //assign pointer for marks as ptr1;
int **ptr2;// double pointer for ptr1(pointer to pointer);
ptr1=&marks; //assign address of marks to ptr1
ptr2=&ptr1;
//find value of variable marks
printf(“value of marks is: %dn”,marks);
printf(“value of marks pointed by ptr1 is: %dn”,*ptr1);
printf(“value of marks point to pointed by ptr2 is: %dn”,**ptr2);
printf(“………………..n”);
//find memory address of variable marks
printf(“address of marks is: %un”,&marks);
printf(“address of marks contain by ptr1 is: %un”,ptr1);
printf(“address of marks using by ptr2 is: %un”,*ptr2);
printf(“………………..n”);
//find value of pointer
printf(“value of pointer ptr1 is: %dn”,ptr1);
printf(“value of pointer ptr1 using ptr2 is: %dn”,ptr1);
printf(“………………..n”);
//find address of pointer ptr1
printf(“address of pointer ptr1 is: %un”,&ptr1);
printf(“address of pointer ptr1 using ptr2 is: %un”,ptr2);
printf(“………………..n”);
//pointer to pointer address and value is
printf(“valueof pointer ptr2 is: %dn”,ptr2);
printf(“address of pointer ptr2 : %un”,&ptr2);
return 0;
}
When the code is executed, it produces the following result
Pointers in C Programming language
Pointers in C++ Programming language
Pointer to pointer in C++ Programming Language
Pointers with one d array in C language
Structure with pointers in C Language
Structure with pointers in C++ Language