Easy rule to read complicated const declarations?

The const modifier is trivial: it modifies what precedes it, unless
nothing precedes it. So:

char const* buffer;  // const modifies char
char* const buffer;  // const modifies *

, etc. Generally, It’s best to avoid the forms where nothing precedes
the const, but in practice, you’re going to see them, so you have to
remember that when no type precedes the const, you have to logically
move it behind the first type. So:

const char** buffer;

is in fact:

char const** buffer;

, i.e. pointer to pointer to const char.

Finally, in a function declaration, a [] after reads as a * before.
(Again, it’s probably better to avoid this misleading notation, but
you’re going to see it, so you have to deal with it.) So:

char * const argv[],  //  As function argument


char *const * argv,

a pointer to a const pointer to a char.

Leave a Comment