Making sense of where "const" goes in a declaration

Viewed 437

I am having trouble finding an intuitive pattern for the way const is used in declarations in the C and C++ languages. Here are some examples:

const int a;    //Const integer
int const a;    //Const integer
const int * a;  //Pointer to constant integer
int * const a;  //Const pointer to an integer
int const * a const;    //Const pointer to a const integer

In lines 1 and 2, it seems const can come before or after int, which is what it modifies.

  1. So how, in line 4, does the compiler decide that const is modifying * (pointer) rather than int?
  2. What is the rule that the compiler follows for deciding which thing the const applies to?
  3. Does it follow the same rule for *?
5 Answers
Related