Pointers to Pointers


Pointers to Pointers
Since every variable declared in a program is assigned an address, similarly when a pointer variable is declared, it is also assigned an address. So it is possible to declare another pointer variable to store this address. The variable used for storing an address of a pointer variable is called a pointer to pointer variable. We can extend this further such as pointer to pointer to pointer variable and so forth. It is often used while passing two or higher dimensional arrays back and forth among different functions. Pointer to pointer offers a great degree of flexibility for handling arrays, passing pointer variables to functions and passing command line arguments.
In pointer to pointer declaration, two pointer variables are declared where one holds single asterisk and second hold double asterisk (**). Pointer to pointer means one pointer variable holds the address of another pointer variable. Again that another variable holds the address of a variable. The hole process is called multiple indirections.
The syntax for declaring a pointer to pointer variable is,
     data_type   **ptr_to_ptr_var_name;
Here, **ptr_to_ptr_var_name is a pointer pointing to a variable of type data_type. The number of asterisks depends on the level of indirection. The number of asterisks keep on increasing as the level of indirection increases. But in practice, it is really needed to exceed a depth of two.
  Now let us consider the following example,
Program to show how pointer to pointer operates.
#include <stdio.h>
#include <conio.h>
void main ()
{
int  i=3, *ptr1, **ptr2;
clrscr ();
ptr1 = &I;
ptr2 = &ptr1;
printf (“Value of i= %d”, i);
printf (“\nValue of *ptr1 = %d”, *ptr1);
printf (“\nValue of **ptr2 = %d”, **ptr2);
getch ();
}
OUTPUT
Value of i = 3
Value of *ptr1 = 3
Value of **ptr2 = 3
Explanation :
In the above program, in the statement
   ptr2 = &ptr1;
ptr2 acts as a pointer pointing to pointer ptr1 which further points to an integer i. Since ptr2 points to a pointer ptr1 so it contains the address where pointer variable ptr1 is stored. So *ptr2 gives the value stored at ptr1 i.e. 65488. This value is actually the address of variable i. This is because ptr1 is a pointer which points to a i so it contains the address of variable i. So *ptr1 gives the value. Stored at i.e. 3. Now we can say that ,
    *ptr2 = ptr1 = &i (=65548)
     *ptr1 = i = 3
implies that
*(*ptr2) = *(ptr1) = *(&i)
or  **ptr2 = *ptr1 = i = 3
·         Another Example of Pointer to Pointer.
 Example of dereferencing pointer.
#include <stdio.h>
#include <conio.h>
void main ()
{
int x =20, *y, **z;
y = &x;
z = &y;
printf (“Value of pointer variable z is: %d”, **z);
getch ();
}
OUTPUT
Value of pointer variable z is: 20
Explanation :
From the above program we saw that z is displaying the value of x by using the operator** called pointer to pointer, e.g., z holds the value of y which again holds the value of x.
Pointer to pointer mechanism

Here in the diagram pointer variable z holds the address of another pointer variable y and again that pointer variable y holds the address of variable x.


No comments

Powered by Blogger.