Original question: If I define:
const int z[5] = {10, 11, 12, 13, 14};
does it mean:
- Each element of z is a constant i.e. their value can never change.
You can't create a const
array because arrays are objects and can only be created at runtime and const
entities are resolved at compile time.
So, the const
is interpreted as in the first example below, i.e. applied for the elements of the array. Which means that the following are equivalent:
The array in your example needs to be initialized.
int const z[5] = { /*initial (and only) values*/};
const int z[5] = { /*-//-*/ };
It is some type commutative property of the const
specifier and the type-specifier, in your example int
.
Here are few examples to clarify the usage of constant:
1.Constant integers definition: (can not be reassigned). In the below two expression the use of const
is equivalent:
int const a = 3; // after type identifier
const int b = 4; // equivalent to before type qualifier
2.Constant pointer definition (no pointer arithmetics or reassignment allowed):
int * const p = &anInteger; // non-constant data, constant pointer
and pointer definition to a constant int
(the value of the pointed integer cannot be changed, but the pointer can):
const int *p = &anInteger; // constant data, non-constant pointer