Pointer to Pointer in C
Pointer to Pointer :
Pointer to Pointer in C is defined when the address of the variable is kept in the first pointer. Additionally, the address of the first pointer is kept in the second pointer.
It is also known as double pointers.
Example 1 :
Run
#include<stdio.h> int main() { int val = 20; int *ptr_1; int ** ptr_2; // declaration of double pointer ptr_1 = &val; ptr_2 = &ptr_1; //printing the value of different location printf("address of val %p\n", ptr_1); printf("address of ptr_1: %p\n", ptr_2); printf("value stored at ptr_1: %d\n", *ptr_1); printf("value stored at ptr_2: %d\n", **ptr_2); return 0; }
Output :
address of val 0x7ffe67b386e4 address of ptr_1: 0x7ffe67b386e8 value stored at ptr_1: 20 value stored at ptr_2: 20
What Happened Above?
Double pointer ptr_2 will print value stored at the address contained by the pointer stored at ptr_2
Example 2
Run
#include<stdio.h> int main() { int val = 20; int* ptr_1 = &val; int** ptr_2 = &ptr_1; // declaration of double pointer printf(" Output of program : %d \n", sizeof(ptr_2)); return 0; }
Output :
Output of program : 8
What Happened Above?
This program will print the size of double pointer in the memory since, we used the sizeof operator in the printf function.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment