Single and multiple indirection.
As you can see, in the case of a normal pointer, the value of the pointer is the address
of a value. In the case of a pointer to a pointer, the first pointer contains the address
of the second pointer, which points to the location that contains the desired value.
Multiple indirection can be carried on to whatever extent desired, but there are few cases where more than a pointer to a pointer is needed, or, indeed, even wise to use. Excessive indirection is difficult to follow and prone to conceptual errors.
A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. For example, this declaration tells the compiler that balance is a pointer to a pointer of type int.
int **balance;
It is important to understand that balance is not a pointer to an integer, but
rather a pointer to an int pointer.
When a target value is indirectly pointed to by a pointer to a pointer, accessing that value requires that the asterisk operator be applied twice, as is shown in this short example:
#include <iostream.h>
#include <stdlib.h>
main()
{
int x, *p, **q;
x = 10;
p = &x;
q = &p;
cout << **q; // prints the value of x
cout << "\n";
system("pause");
return 0;
}
Here, p is declared as a pointer to an integer, and q as a pointer to a pointer to an integer. The cout statement will print the number 10 on the screen.
Last Updated Jul.15/99