5.5. Pointers to pointers¶
A pointer can point to any memory address within the scope of the program, which includes pointers themselves. Each new pointer just adds another to the chain of pointers. The language does not impose a strict limit. The only limit is your sanity…
int x = 8;
// all of these variables point to x
int* p2x = &x;
int** p2p = &p2x;
int*** p2pp = &p2p;
Like int
or char
, a pointer type is still a type.
When you declare a variable of type pointer,
storage still must be allocated somewhere,
and this storage must have an address too.
When dealing with pointers, we have to manage the added complexity of keeping clear in our minds the difference between the pointer variable and what the pointer points to. When dealing with pointers to pointers, we have to manage the pointer, what it points to, and what the pointer that it points to points to.
#include <iostream>
#include <string>
using std::string;
using std::cout;
int main() {
string message[] = {"Alice","Bob here!","Carol checking in."};
string *sp; // a pointer to at least 1 string
sp = message;
cout << "sp:\n";
cout << sp << '\n';
cout << *sp << '\n';
cout << *(sp + 1) << '\n';
cout << *(sp + 2) << "\n\n";
cout << "sp2:\n";
string *sp2 = new string [3]; //create string pointer on the heap
*sp2 = "\nAlice has left the building";
*(sp2 + 1) = "Bob who?";
*(sp2 + 2) = "Carol checked out.";
cout << sp2 << '\n';
cout << *sp2 << '\n';
cout << *(sp2 + 1) << '\n';
cout << *(sp2 + 2) << '\n' << '\n';
string **sp3; // a pointer to a string pointer
cout << "sp3:\n";
sp3 = &sp2;
cout << sp3 << '\n';
cout << **sp3 << '\n';
}
You can also define a pointer to a reference variable:
More to Explore