My professor gave me this code and told me that it has "Undefined Behaviour":
#include <iostream>
int main()
{
const int WEEKDAYS = 7;
const int *pWeekdays = &WEEKDAYS;
*((int*)pWeekdays) = 9;
std::cout << WEEKDAYS;
std::cout << *pWeekdays;
return(0);
}
It gives the following output:
79
I am trying to understand the following:
- Why is the behaviour undefined?
- Why does printing
*pWeekdays
output9
but printingWEEKDAYS
outputs7
?