Remember that a pointer is just a variable containing the memory address of another variable. A pointer to a pointer is no different, other than that the address contains the address of another pointer. You use the * indirection operator to get the value of the variable being pointed at (the address of the other pointer), and the ** indirection operator to get at the value pointed at by the other pointer.
The following example illustrates how to access the values of pointers to int via an array of pointers to those pointers.
The memory address and the value of every variable is displayed for the benefit of clarity.
#include <iOStream>
using namespace std;
int main()
{
// Set up an array of pointers to pointers to int variables.
int X = 1, Y=2; // The actual variables.
int* pX = &X; // Pointers to those variables
int* pY = &Y;
int** pp[2]; // Array of pointers to those pointers.
pp[0] = &pX;
pp[1] = &pY;
// Print the address of all variables and their stored values:
cout << "Var\t&Address\tValue" << endl;
cout << "---\t--------\t-----" << endl;
cout << "X\t0x" << &X << "\t" << X << endl;
cout << "Y\t0x" << &Y << "\t" << Y << endl;
cout << "pX\t0x" << &pX << "\t0x" << pX << endl;
cout << "pY\t0x" << &pY << "\t0x" << pY << endl;
cout << "pp\t0x" << &pp << "\t0x" << pp << endl;
cout << endl;
cout << "Note that both &pp and pp return the same value: the address of the array." << endl;
cout << "pp is simply an alias for the memory allocated to the array itself, it is" << endl;
cout << "not a variable that contains a value. You must access the elements of the" << endl;
cout << "array to get at the actual values stored in the array." << endl;
cout << endl;
// Use the array elements to access the pointers and the values they point to:
cout << "Elem\t&Address\tValue\t\t*Value\t\t**Value" << endl;
cout << "----\t--------\t-----\t\t------\t\t-------" << endl;
cout << "pp[0]\t0x" << &pp[0] << "\t0x" << pp[0] << "\t0x" << *pp[0] << "\t" << **pp[0] << endl;
cout << "pp[1]\t0x" << &pp[1] << "\t0x" << pp[1] << "\t0x" << *pp[1] << "\t" << **pp[1] << endl;
cout << endl;
return( 0 );
}
Copyright © 2026 eLLeNow.com All Rights Reserved.