Consider the following array declaration:
int const a[5];
From the semantic standpoint of the language, is it exactly equivalent to const int a[5]
? Assuming that is the case, both declarations would essentially read like "a is an array of 5 constant ints".
The alternative way to read the first declaration would be "a is a constant array of 5 ints".
Obviously, both of the statements logically imply that the whole array is constant; if an array consists of 5 constant ints, then the entire array is constant. Alternatively, if the whole array is constant, then all of its values are also constant.
I am aware that the notion of a "constant array" is a bit meaningless since arrays are not modifiable lvalues (that is, they cannot appear on the left side of an assignment). However, are there any circumstances under which these two declarations would yield different behaviour?
(Cdecl.org rejects the first declaration as a syntax error, while most current compilers accept it.)
EDIT:
The linked duplicate asks whether the order of const
matters for ordinary variables. With arrays, it is a bit more confusing, so I don't consider this a duplicate.