I read that in link, that assigning address of const int * to int * is illegal for C++ and throws compiler error. I tried assigning the const int * to int * by explicit cast and tried changing the value. I couldn't understand the program behavior. I was wondering if it is undefined behavior or there is some reasoning behind it.
#include <stdio.h>
int main()
{
int const i = 20;
int * ptr;
ptr = (int *)&i;
*ptr = 500;
printf("%d %d %p %p\n", *ptr, i, ptr, &i);
return 0;
}
The output was
500 20 0x7ffe16ea5134 0x7ffe16ea5134