Pointer to a Pointer in C
Like any other variables, pointer variables also have their own address and this means we can store it in another pointer variable.
Normally to declare a pointer variable we write ‘* asterisk’ sign before the pointer variable.
E.g.
int *ptr;
But in now we have to create a pointer which can store address of another pointer so to declare such pointer we need to add one extra ‘* asterisk’ before the variable.
E.g.
int *ptr // this is normal pointer
int **ptr2 // this is pointer to a pointer which can store address of another pointer
Note :- We can create pointer variable using three ‘* asterisk’ sign before it, and this pointer variable can store the address of a pointer which stores address of another pointer which stores address of variable but it is not recommended.
Let’s suppose we store an address of int variable x in pointer ptr and then store address of pointer ptr in pointer ptr2.
This will create a chain, we can also access the value of int variable x form ptr2.
If we write ptr2 without any ‘* asterisk’ sign before it i.e. ptr2 then it will print address of ptr and if we use two ‘* asterisk’ sign before ptr2 i.e. **ptr2 then it will print the value of in variable x.
Example of Pointer to Pointer in C
#include <stdio.h>
int main ()
{
int x = 10, *ptr, **ptr2;
ptr = &x;
ptr2 = &ptr;
printf(" x = %d | *ptr = %d | **ptr2 = %d",x, *ptr, **ptr2);
printf("n &ptr = %d | ptr2 = %d",&ptr, ptr2);
return 0;
}
Output:
x = 10 | *ptr = 10 | **ptr2 = 10
&ptr = 6422308 | *ptr2 = 6422308
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM
