As we know the value of constant variable is immutable. But we can use the pointer of constant variable to modify it.
#include <iostream>
int main()
{
const int integer = 2;
void* tmp = (void*)&integer;
int* pointer = (int*)tmp;
(*pointer)++;
std::cout << *pointer << std::endl;
std::cout << integer << std::endl;
return 0;
}
the output of that code is:
3
2
So, I am confusing what i modified on earth? what does integer stand for?
Modifying
consts is undefined. The compiler is free to storeconstvalues in read only portions of memory and throw error when you try to change them (free to, not obliged to). Undefined behavior is poor, undesirable and to be avoided. In summary, don't do that.PS
integerandpointerare variable names in your code, tho not especially good names.