Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to change a const data value?

can someone explain to me why does this code doesnt change the value of a?
is it possible to change a const data value?

case 1:

const int a = 2;
*((int*)&a) = 3;
std::cout << a;

case 2:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const int a = 2;
const_cast<int&>(a) = 3;
std::cout << a;

>Solution :

Changing a const value is undefined behavior.
I would advise you not to do it or write any program that does so.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading