+ 2

How to change value of constant in C

31st Oct 2017, 12:51 PM
Shivam Pandey
Shivam Pandey - avatar
4 Antworten
+ 2
Attempting to modify a constant variable is undefined behavior. It could be placed in read only memory or optimized away by the compiler since it's value is constant, so you shouldn't try to do it.
31st Oct 2017, 1:52 PM
aklex
aklex - avatar
+ 1
const int x = 5; int* p = (int*)&x; *p = 22; Still undefined behavior but that's how it can be done
31st Oct 2017, 1:59 PM
aklex
aklex - avatar
+ 1
Neither method in this stack overflow answer appears to work here; half the other stuff I tried aborts the runtime somehow (causing no output). https://stackoverflow.com/a/583104 Emphasis on 'undefined behavior' as noted in @aklex's answers.
31st Oct 2017, 2:59 PM
Kirk Schafer
Kirk Schafer - avatar
0
I have read in many clauses of C books that you can change change constant value in c using pointers but how it wasn't given
31st Oct 2017, 1:54 PM
Shivam Pandey
Shivam Pandey - avatar