Pointer to Pointer in C.
Hello Everyone,
In Previous blog we discussed about pointers in C. Today we will discuss about Pointer to Pointer in C. Before starting this topic please go through my previous blog as below for your reference.
https://dheeraj60.blogspot.com/2020/06/basic-concept-of-pointer-in-c-and-its.html
Let’s Start with Pointers to Pointer
As we know now pointer is basically used to find the address of variable. When a pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value.
Syntax for Declaring Pointer to Pointer
**p;
In Pointer we use to take 1 *, but in pointer to pointer we need to take 2 * to get the address of second pointer and the location that contains the actual value.
Let’s take 1 simple example to understand Pointer to Pointer Program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=100;
int *p;
int **p1;
p = &a;
p1 = &p;
/* Possible ways to find value of variable num*/
printf("\n Value of num is: %d", a);
printf("\n Value of num using pointer1 is: %p", p);
printf("\n Value of num using pointer2 is: %p", p1);
return 0;
}
Here * p is normal pointer which we used in previous blog to get the address of variable and int **p1 is Pointer to pointer .
p = &a; we used to Assigning the address of variable a to the pointer p and p1=&p used to assigning the address of pointer p1 to the assigning the address of pointer p to the pointer to pointer.
So the output of the program we can see three other value one is variable value which we assigned and another address of variable and the last one we will see the pointer to pointer value.
Below is the output of above Program in Compiler Code Block.
Here we are able to see all the three different values of variables.
Note: Pointer to Pointer in C. We already know that a pointer points to a location in memory and thus used to store the address of variables. 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.
Let’s understand one more program to see all probable values by using Pointer to Pointer.
Program is as below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=10;
int *p;
int **p1;
p=&a;
p1=&p;
printf("Address of number variable is %x \n",&a);
printf("Address of p variable is %p \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p1 variable is %p \n",p1);
printf("Value of **p1 variable is %d \n",**p1);
return 0;
}
Here we will see the actual variable value and also we can see the address of pointer and pointer to pointer with actual value as well.
Below is the output of above program in Compiler Code Block.
Here we can see all the probable values of variables with address and pointer to pointer value as well with actual value. Hope you are able to understand Pointers to Pointers now .
Advantages of pointers are
1. Pointers allow management of structures which are allocated memory dynamically.
2. Pointers allows passing of arrays and strings to functions more efficiently.
3. Pointer makes possible to pass address of structure instead of entire structure to the functions.
Pointer is commonly used in Functions and Array . As of now i haven’t started Functions in C so in next blog i will come up with Function in C and will use Pointer later by using Functions . Pointer is commonly used in Functions and Arrays. So please go through this blog carefully and if any doubt let me know.
Functions plays very important role in C Program so in next blog i will come up with Function in C and later we can use Pointer in Function also. So please try to understand this blog as later we will use this in Functions as well.
Thanks.
Informative & useful for IT students.
ReplyDelete