This is not an attempt to change i , which leads to the fact that the behavior of your program will be undefined, this is the initialization of ip .
const int i = 11; int *ip = &i;
&i is of type const int* . ip is of type int* . Attempting to initialize int* const int* is a violation of the constraint. An appropriate implementation requires diagnostics; as soon as he does, he may or may not reject the translation block. If it accepts this, the C standard says nothing about the resulting behavior of the program.
Compilers that accept such things usually generate code equivalent to converting from const int* to int* , which makes the declaration virtually equivalent:
int *ip = (int*)&i;
but language does not require such behavior.
Do not ignore warnings.
(Note that with cast code does not violate the restriction, and then the behavior of the following
*ip = 100;
is undefined because it is trying to modify the const -qualified object.)
In particular, gcc has many cases in which the diagnosis of constraint violations is treated (by default) as warnings rather than fatal errors. I personally don't like this in gcc; it misses too much bad code.
(The behavior of your program is also undefined, because you call printf without a visible declaration, add #include <stdio.h> . And int main() should be int main(void) .)
source share