which property of a constant makes it not changable?
c, c++
Solution
If he said this is fine, then he was wrong: trying to modify a constant object gives undefined behaviour. In practice, one of three things might happen:
- The constant variable behaves just like a normal object, and you see its value change;
- It's stored in unwritable memory, and the program crashes with an access violation;
- Each use of it is replaced with a hard-coded value, and you don't see it change.
The language doesn't define any run-time properties of `const` objects; just compile-time checks that you don't accidentally modify them.
Problem
Today I faced an interview in which one question was very tricky for me. Interviewer said "how to make constant able to change its value?" I replied "using pointer" and I shown him an example : ``` int main( void ) { const int a = 3; int *ptr; ptr = (int*)( &a ); printf( "A=%d\n", a ); *ptr = 5; printf( "A=%d\n", a ); return 0; } ``` But he said this is fine. But tell me which is property which makes constant non changeable? and he also said that there is one property which we can change and make constant changeable. Is there any property like that? How does it work?