I am sure that the following code should not compile. But, in g++, it does compile! See it compile at http://codepad.org/MR7Dsvlz .
The code:
#include <iostream> using namespace std; int main() { int x = 32 ; // note: if x is, instead, a const int, the code still compiles, // but the output is "32". const int * ptr1 = & x ; *((int *)ptr1) = 64 ; // questionable cast cout << x ; // result: "64" } Is g++ in error by compiling this?
const_cast<int*>(ptr1)- although the C cast will work too, as you've just seen.