Learn Pointer to a Pointer in C in easy way – 4

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

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment