Let’s look at what is meant when const
is used. It’s really quite simple:
const means that something is not
modifiable, so a data object that is
declared with const as a part of its
type specification must not be
assigned to in any way during the run
of a program. It is very likely that
the definition of the object will
contain an initializer (otherwise,
since you can’t assign to it, how
would it ever get a value?), but this
is not always the case. For example,
if you were accessing a hardware port
at a fixed memory address and promised
only to read from it, then it would be
declared to be const but not
initialized.
Taking the address of a data object of a type which isn’t const and putting it into a pointer to the const-qualified version of the same type is both safe and explicitly permitted; you will be able to use the pointer to inspect the object, but not modify it. Putting the address of a const type into a pointer to the unqualified type is much more dangerous and consequently prohibited (although you can get around this by using a cast). For example…