I understand that implicit conversion from non-const
to const
is not dangerous when handling values, by example:
int mutable = 5;
const int immutable = mutable;
However, when working with pointers, I can do something as follows:
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[0] = 10;
printf("%d\n", *immutable); // Prints 10
By the way, for double pointers this is not allowed (at least you get a warning)! See this question and the references therein.