The compiles sees
i is declared as
const and so expects it to never change value. And such, in
cout<<i, the compiler is allowed to optimize that call by substituting
i for its declared value, as if you had written
cout<<10; That is why you are seeing different values being output.
*p=11 is invoking undefined behavior since you are modifying the value of a
const. Don’t do that.
Leave an answer