Item 8. Pointers to Pointers

It’s legal to declare a pointer to a pointer. This is what the C++ standard calls a “multilevel” pointer.

int *pi; // a ptrint **ppi; // a two-level multilevel ptrint ***pppi; // a three-level multilevel ptr

Although it’s rare to encounter multilevel pointers with more than two levels, we do see pointers to pointers in two common situations. The first is when we declare an array of pointers.

Shape *picture[MAX]; // array of ptr to Shape

Because an array name decays into a pointer to its first element (see Array Formal Arguments [6, 17]), the name of an array of pointers is also a pointer to a pointer.

Shape **pic1 = picture;

We most often see this usage in the implementation of a class that manages a buffer of pointers: ...

Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.