For below snippet,
#include <iostream>
using namespace std;
void fun(const int *p)
{
int *q = const_cast<int *>(p);
*q = *q * 10;
cout<<"q: "<<q<<" Value: "<<*q<<endl;
}
int main()
{
const int a = 10;
const int *z = &a;
fun(z);
cout<<"z: "<<z<<""<<"Address of a: "<<&a<<endl;
cout<<"value at z: "<<*z<<" value in a: "<<a<<endl;
}
the output produced is
q: 0x7fff65910fcc Value: 100
z: 0x7fff65910fcc Address of a: 0x7fff65910fcc
value at z: 100 value in a: 10
Why the value of a is not modified even though i tried to modify it in fun()?
How come the address of a and the pointer z are same but the values are different?
Is it some kind of undefined behavior with const_cast ?
See Question&Answers more detail:os