I am trying to understand const in c++. I wrote this following code snippet:
const int x=5;
int *ptr;
ptr=(int*)&x;
cout<<"address of x="<<&x<<endl;
cout<<"value of ptr="<<ptr<<endl;
*ptr=11;
cout<<"*ptr="<<*ptr<<endl;
cout<<"x="<<x;
The output is
address of x=0x28fef8
address of ptr=0x28fef8
*ptr=11
x=5
Since ptr is pointing to x, i was sure the value of the *ptr and x would be the same. Why are the values different? I understand that x is const, however, i am changing the value at the memory address by doing *ptr . Please tell me what am i missing.