Pointer To A Pointer In C Programming Language


Lets write C program to learn the concept of pointer pointing to another pointer. It’s also called as Multiple Indirection or double pointer concept.

Note: We could go any level deep. For Example, a pointer can point to a pointer which is pointing to another pointer, which is pointer to a pointer pointing to another pointer and so on.

But only 2 concepts hold practical usage in our programs:
1. The pointer concept.
2. Pointer to Pointer concept.

Related Read:
Basics of Pointers In C Programming Language

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Example:

int num = 5;
int *ptr1, **ptr2;

ptr1 = #
ptr2 = &ptr1;

According to above code, ptr1 holds the address of variable num. So *ptr1 should fetch the value present at the address num. If you print out the *ptr1 you’ll get 5 as the result.

Similarly, ptr2 holds the address of pointer variable ptr1. *ptr2 fetches the value present at the address ptr1 – which is address of num. If we print out *(*ptr2) it prints out the value present at num, which is 5.

Video Tutorial: Pointer To A Pointer In C Programming Language


[youtube https://www.youtube.com/watch?v=p9KzkDR8dxc]

YouTube Link: https://www.youtube.com/watch?v=p9KzkDR8dxc [Watch the Video In Full Screen.]


Source Code: Pointer To A Pointer In C Programming Language

#include<stdio.h>

int main()
{
    int num = 5;
    int *ptr1, **ptr2;

    ptr1 = &num;

    printf("Num = %d\n", num);
    printf("Address of Num, using variable num = %d.\n", &num);
    printf("Address of Num, using pointer ptr1 = %d.\n", ptr1);
    printf("Value present at Address %d is %d.\n\n", ptr1, *ptr1);

    ptr2 = &ptr1;

    printf("Address of pointer variable ptr1 = %d\n", ptr2);
    printf("Value present at address %d is %d, 
           which in turn holds the value %d.\n", ptr2, *ptr2, **ptr2);

    return 0;
}

Output:
Num = 5
Address of Num, using variable num = 6356728.
Address of Num, using pointer ptr = 6356728.
Value present at Address 6356728 is 5.

Address of pointer variable ptr1 = 6356724
Value present at address 6356724 is 6356728, which in turn holds the value 5.

Homework: Do it on your own

According to above program

int ***ptr3;
ptr3 = &ptr2;

printf("%d", ***prt3);

What will be the output?

Note: Take pen and paper. First know the value present at ptr3. Next check the result for *ptr3. Then check for **ptr3 and then finally ***ptr3.

Hint:
ptr3 holds address of ptr2.
i.e., ptr3 = &ptr2;

*ptr3 can be writte an *(&ptr2).

*(&ptr2) will fetch the value present at address &ptr2, which is address of pointer variable ptr1.
i.e., *(&ptr2) = &ptr1.

Next, *(&ptr1) will fetch the value present at &ptr1, which is address of num.
i.e., *(&num) which is equal to 5.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *