I understand that implicit conversion from
int mutable = 5;
const int immutable = mutable;
int some_number = 5;
int *mutable = &some_number;
const int *immutable = mutable; // <= Legal, but isn't it dangerous?
// Let's try to break const
printf("%d\n", *immutable); // Prints 5
mutable = 10;
printf("%d\n", *immutable); // Prints 10
6.7.3 Type qualifiers
- The properties associated with qualified types are meaningful only for expressions that are lvalues.
An object declared
extern const volatile int real_time_clock;
may be modifiable by hardware, but cannot be assigned to, incremented, or decremented.
In simple terms:
const doesn't mean a value never changes. It only means that you are not allowed to change it.
For callees ,
const is a restriction, not a promise.
For callers however, it is a promise. Passing a
const pointer to a function, you can safely assume that the function will not change your data, thus is "making a promise to you".