Having to follow code –
const int WEEKDAYS = 7;
const int *pWeekdays = &WEEKDAYS;
*((int*) pWeekdays) = 9;
cout << WEEKDAYS << endl;
it give output 7 , that is – the line *((int*) pWeekdays) = 9; had no effect or thrown any errors .
Apparently it’s like to do 7 = 9 so why no errors are being thrown ?
Casting away constness and thus accessing a const object mutably is simple undefined behaviour. Your program can do anything it wants, and no diagnostic is required.
Always remember that while it’s true that a crashing program is buggy, a buggy program doesn’t always crash. (Or as Socrates would have said, “not every cat is a fish”.)
In C++ you should really never use C-style casts. If you had tried the more appropriate
static_cast<int*>(pWeekdays), you would have got the correct diagnostic.