In C ++, we know that we cannot convert const int * to int *. But I have a piece of code where I can convert const int * to int *. I am new to C ++, I googled for this, but I just got links mentioning that const int * cannot be converted to int * to avoid breaking const. I cannot understand why it compiles 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;
}
In addition, the problem is that the output of the above program:
0x7fff5476db8c 0x7fff5476db8c
43 40
Can someone explain that c1 is an integer pointer pointing to the same address for a1, but having different values of 43 and 40, respectively.
source
share