In c++, we know that we can't convert const int* into int*. But I have a code snippet where I am able to convert const int* into int*. I am a beginner in c++, i googled for this but i just got the links mentioning const int* can't be converted into int* to avoid const violation. I am not able to figure out why is it compiling without errors
#include <iostream>
using namespace std;
int main(void)
{
const int a1 = 40;
const int* b1 = &a1;
int* c1 = (int *)(b1);
*c1 = 43;
cout<< c1<<" "<< &a1<<endl;
cout<< *c1<<" "<< a1<<endl;
}
Also, the problem is the output of the above program is:
0x7fff5476db8c 0x7fff5476db8c
43 40
Can someone please explain c1 integer pointer is pointing to the same address for a1 but having different values 43 and 40 respectively.