Pointer to Pointer In C [Explained With Examples] – CsTutorialpoint
In today’s article, we are going to talk about Pointer-to-Pointer or double pointer In C
Today we will learn in detail about, what is Pointer to Pointer In C. And how and why is it used in C language.
So now without wasting time let’s understand Pointer-to-pointer or double pointer In C Language.
Pointer to Pointer In C
As we know, Pointer is a variable which is used in C language to contain the address of another variable.
Since the pointer is also a variable so it also takes some space in memory and it also has an address. And when we have to contain the address of this pointer variable then we need a pointer variable that can contain the address of this pointer variable.
This pointer variable, which contains the address of another pointer variable, is called Pointer to Pointer or double Pointer variable.
Pointer to pointer is commonly used when passing pointer variables to functions.
Syntax -:
data_type **pptr
- data_type -: Here the data type can be anything. If we want to store the address of pointer variable of int data type then here int will come and if we want to store address of pointer variable of float type or char type then here float or char will come.
- **pptr -: This is the name of the pointer to pointer variable that will contain the address of another pointer variable (which points to a normal variable).
Note -: While declaring Pointer to Pointer variable, double asterisk (**) symbol is used so that it can be shown different from the normal pointer variable.
Let’s understand the Pointer to Pointer variable with this one example -:
int a = 5;
int *pa = &a;
int **ppa = &pa;
Here a is a normal variable of int type. To store the address of this int type variable, a pointer variable named pa is created which is storing the address of a variable. This pointer variable pa is declared using the asterisk(*) symbol to make it stand out from the normal variable.
pa pointer variable will also have an address because it is also a kind of variable so to store its address into another variable named **ppa is declared with double astric symbol so that this **ppa variable stores the address of the *pa variable.
Let’s understand these things through a program.
Example Program
//Program to understand pointer to pointer
#include<stdio.h>
void main( )
{
int a=5;
int *pa;
int **ppa;
pa=&a;
ppa=&pa;
printf("Address of a = %u\n", &a);
printf("Value of pa = Address of a %u\n",pa);
printf("Value of *pa = Value of a = %d\n",*pa);
printf("Address of pa = %u \n", &pa) ;
printf("Value of ppa = Address of pa = %u\n" ,ppa);
printf("Value of *ppa = Value of pa = %u\n", *ppa);
printf("Value of **ppa Value of a = %d\n",**ppa);
printf("Address of ppa = %u\n",&ppa);
}
Output
Address of a = 6422268
Value of pa = Address of a 6422268
Value of *pa = Value of a = 5
Address of pa = 6422264
Value of ppa = Address of pa = 6422264
Value of *ppa = Value of pa = 6422268
Value of **ppa Value of a = 5
Address of ppa = 6422260
So, friends, it may be that after seeing the program mentioned above, you may not understand what is happening in the program.
To understand this program, you must have good knowledge about address and asterisk operator. You can read about these from the link given below.
- Address and Indirection (Asterisk) Operator In C
Friends, to know about Pointer to pointer in C, you can watch this video given below once. Even after this, if you have any problem in understanding this topic or have any questions, then tell me in the comment below, I will definitely answer your questions.
Read More -:
- Introduction of Pointer in C
- Address Operator (&) In C
- Indirection Operator(*) In C
- Pointer Arithmetic In C
- Pointer to Structure in C
- Dangling Pointer in C
- Wild Pointer In C
- Null Pointer In C
- Void Pointer In C
- Dynamic Memory Allocation in C Programming
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is Pointer to Pointer or Double Pointer In C and what is its use in the C language.
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Pointer to Pointer In C
To get more information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.