Consider the following declarations:
int * const bla;int const * bla; const int * const bla;
As you can see, it can sometimes be hard to remember whether it’s the pointer that is declared constant or whether the pointer points to a constant variable or both.
The trick to deciphering these is to read the declaration backwards from right to left. For instance:
- int * const bla; reads as: bla is constant and points to an int.
- int const * bla; reads as: bla points to a const int. Specifically, bla can be changed but only points to integers that are const.
- int const * const bla; reads as: bla is constant and points to a constant int. Specifically, bla is a constant pointer that can only point to a const int and neither can be changed.
Specifically, bla is a constant pointer that can’t be changed and always points to the same int.
Just remember: read backwards from right to left.