Undefined behaviour of const
c, c++
Solution
The optimizer can determine that `a` is a constant value, and replace any reference to it with the literal `3`. That explains what you see, although there's no guarantee that's what's actually happening. You'd need to study the generated assembly output for that.
Problem
I never thought I will be going to ask this question but I have no idea why this happens. ``` const int a = 3; int *ptr; ptr = (int*)( &a ); printf( "A=%d\n", &a ); *ptr = 5; printf( "A=%d\n", ptr ); printf( "A=%d\n", a ); printf( "A=%d\n", *ptr ); ``` Output ``` A=6945404 A=6945404 A=3 A=5 ``` How can this happen? How can one memory location hold two different values? I searched around and all I find is undefined behavior is undefined. Well that does not make any sense. There must be an explanation. Edit I get it, `Marks` answer makes alot of sense but still I wonder that const was added into the language so that user does not change the value unintentionally. I get that old compilers allows you to do that but I tried this on VS 2012 and I got the same behavior. Then again as `haccks` said, one memory location can't hold two values it looks like it does, then where is the second value stored?