operations on constatnt variables

#include<iostream>
using namespace std;
void fun( const int &k)
{
cout<<k;
}

int main()
{
const int i=10;

int *p=(int*)&i;
*p=15;
cout<< i;
cout<<*(&i);
cout<<*p;
fun(i);

}


please explaian me the logic behind the output.....

The output is 10101515
Fortunately for you, your compiler stored the constant variable in a writable section of memory (which it only does if you ask for the address of the variable) so it happened to be writable and not cause a seg-fault.
I think this is what is happening, correct me if I am wrong:
The 10 come from the compiler replaces some instances of i with 10 at compile time, however in other places it must use real memory (which you have illegally changed to 15).

EDIT: Snipe'd by firedraco
Last edited on
Topic archived. No new replies allowed.