*
co******@gmail.com:
>
Can anyone help me where exactly at what address the *p is storing the
updated value ( which is 99).
const int k=9;
int *p=(int *)&k;
cout<<"Addr of k : "<<&k<<" "<<p;
*p=99; //here *p has updated the value of k
cout<<"Addreses\n"<<&(*p)<<&k; //BOTH the addresses are same.
cout<<"values \n"<<*p<<k; //But still the values are differentl.
what exaclty is happening here...??
Undefined Behavior (UB), because: modifying an originally const variable.
Or, practically speaking, that you have told the compiler that it can
rely on k being constant, so that it can use the original value directly
wherever it's needed. When you request the address of k you get the
address of a location containing that value. But that doesn't mean the
compiler needs to (generate code to) retrive the value from that
location where the value is used; you've explicitly told the compiler
that it doesn't need to, that the value is constant, unchanging.
However, formally none of that reasoning holds, because you have UB.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?