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.

Pointer to Pointer in C

Syntax :

Data_Type **  variable_name

Representation of Pointer to Pointer :

Pointer to Pointer in C

Uses of Pointer to Pointer in C :

  • We can easily accesss 2-D array in C using double pointer.
  • Command line argument in C
  • Double pointer can also be used in dynamic memory allocation.

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

						

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 

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription