Learn Pointer to Pointer in C

pointer to pointer


As we already know that pointer points to a memory location which store the address of a variable. So, What is Pointer to Pointer?

A pointer which holds the address of another pointer is known as a pointer to pointer. It is also known as pointer-pointer or double pointer.

See Also: Types of Pointers in C

Pointer to Pointer


As we can see in the above figure, pointer 'Ptr1' holds the memory address of variable 'W' and pointer 'Ptr2' holds the memory address of pointer 'Ptr1'. So, pointer 'Ptr2' is called Double Pointer in C.


How to Declare a Pointer to Pointer in C?

A double pointer is declared similarly as a pointer in C. The difference is that in Double pointer we use an additional indirection operator (*) before the name of the pointer.

Syntax

int **ptr1;

Following example help you to understand the concept of the pointer to pointer in C.
#include <stdio.h>
int main()
{
    //Declare variable 'w'.
    int w=10;
    //Declare pointer variable 'ptr2'
    int *ptr2;
    //Declare double pointer variable 'ptr1'
    int **ptr1;
    //Assigning the address of variable 'w' to the pointer variable 'ptr2'
    ptr2 = &w;
    //Assigning the address of pointer variable 'ptr2' to the double pointer variable 'ptr1'
    ptr1 = &ptr2;

    //Number of ways to find the value of the variable 'w'
    printf ("\n Value of w is :%d",w);
    printf ("\n Value of w using ptr2: %d", *ptr2);
    printf ("\n Value of w using ptr1 :%d \n\n", **ptr1);

    //Number of ways to find the Address of variable 'w'
    printf ("\n Address of w is :%p\n",&w);
    printf ("\nAddress of w using ptr2 is: %p\n",ptr2);
    printf ("\n Address of w using ptr1 is:%p\n",*ptr1);

    //Value of pointer ptr2
    printf ("\n Value of pointer ptr2 : %p", ptr2);
    // Address of pointer ptr2
    printf ("\n Address of pointer ptr2:%p",&ptr2);
    printf ("\n Address of ptr2 using pointer ptr1: %p",ptr1);
    //Address of pointer ptr1
    printf ("\n Address of pointer ptr1: %p",&ptr1);
    //Value of pointer ptr1
    printf ("\n Value of pointer ptr1: %p\n\n",ptr1);
    return 0;
}

Output

Value of w is: 10
Value of w using ptr2: 10
Value of w using ptr1: 10
Address of w is: 0028FF1C
Address of w using ptr2 is: 0028FF1C
Address of w using ptr1 is: 0028FF1C
Value of pointer ptr2 is: 0028FF1C
Address of pointer ptr2: 0028FF18
Address of ptr2 using pointer ptr1:0028FF18
Address of pointer ptr1: 0028FF14
Value of pointer ptr1: 0028FF18

You can understand the program by these equations:
Value of w ==*ptr2==**ptr1
Address of w ==ptr2 == *ptr1
&ptr2 ==ptr1

Find Out the Output of the Following Program Using Pointer to Pointer or Double Pointer in C.

#include <stdio.h>
int main()
{
    int arr[]={10,20,30,40,50,60};  //Line-1
    int *ptr[]={arr,arr+1,arr+2,arr+3,arr+4,arr+5};  //Line-2
    int **qtr=ptr //Line-3
    qtr++;  //Line-4
    printf ("%d\n",**qtr);   //Line-5
    *qtr++;   //Line-6
    printf ("%d\n",**qtr);  //Line-7
    ++*qtr //Line-8
    printf ("%d\n",**qtr);  //Line-9
    ++**qtr;  //Line-10
    printf ("%d\n",**qtr);  //Line-11
    return 0;
}


pointer to pointer

Explanation

In the above program, double pointer(**qtr) work as an arithmetic pointer. An integer type array (arr) is declared. Pointer (*ptr) consists of a base address of array(arr).
Line 1: An integer type array 'arr' with 6 elements are declared.

Line 2: An array of pointer *ptr[] is declared which holds the address of array arr[].

Line 3: Double pointer (**qtr) holds the address of pointer *ptr.
If the address of the array starts from 2000 and the size of the integer is 2. The pointer array 'ptr' will contain the values like 2000,2002,2004,2006,2008,2010. Assume, the base address of pointer array 'ptr' starts from 3000. The double pointer (*qtr) holds the address of pointer array (*ptr).

Line 4: Value of 'qtr' is increased by one i.e. double pointer (**qtr) points to address of pointer array i.e. 3002.

Line 5: printf ("%d\n", **qtr);
*qtr==*ptr==arr[1]
Double pointer (**qtr) holds the address of (*ptr) i.e. 3002 and Pointer (*ptr) holds the address of array i.e. 2002.
qtr = 3002, *qtr =2002, *(*qtr)=*(2002)=20 i.e. output will be 20 at the output screen.

Line 6: As we can see, *qtr++ is written. Here, are the two operators * and ++ operator. But, Both have the same precedence associativity so it will evaluate from right to left. *qtr++, qtr hold the address 3002 i.e. (*(qtr++))==(*3004))==2004.

Line 7: printf ("%d\n", *qtr) i.e. *qtr==*(2004)==3000.

Line 8: ++*qtr can be rewritten as according to the rule of associativity, (++(*qtr)). Since, qtr = 3004, *qtr=2004, ++*qtr = 2004+2=2006.

Line 9: printf ("%d\n", **qtr), *qtr=2006=*(2006)=40

Line 10: ++**qtr can be written as (++(*(*(qtr)))), qtr = 3004, *qtr=2006, **qtr=40, ++**qtr = **qtr+1=41.

Line 11: printf ("%d\n", **qtr); 41 will print at the output screen.

Learn Pointer to Pointer in C Learn Pointer to Pointer in C Reviewed by Glory on 09:41 Rating: 5

No comments:

Powered by Blogger.