How do I read C++ pointer construct?

How do I read these constructs?

Read them backwards and read the * as "pointer to".

const int* const* const

is a constant pointer to a constant pointer to an integer constant.

const int**

is a pointer to a pointer to an integer constant.


It gets a bit easier if you group things the right way. For example, *const is really one unit meaning "const pointer to" (you can read the const as a subscript here: *const). I'd write it as:

const int *const *const p1; // p1 is a const pointer to const pointer to const int
const int **p2; // p2 is a pointer to pointer to const int

Also remember that declarations read "inside out", starting at the identifier being declared.


There is a tool that's useful/fun to decipher declarations: http://cdecl.ridiculousfish.com/

In your case it reports: const int* const* const x => declare x as const pointer to const pointer to const int const int** x => declare x as pointer to pointer to const int

Tags:

C++

Pointers