0

I was testing this code below and expected compiler to throw runtime exception because I am changing value of v, which refers to the same address as intV which is a constant. However, I got the following output

4
5
4
0x7ffea6823a44
0x7ffea6823a44

Could someone pelase explain how come address of intV is same as v but the value of intV is different from value of *v?

int main(char ** args, int argc)
{
  const int intV = 4;
  int *v = const_cast<int*>(&intV);
  *v = 5;
  std::cout << intV << std::endl;
  std::cout << *v << std::endl;
  std::cout << intV << std::endl;
  std::cout << v << std::endl;
  std::cout << &intV << std::endl;
}

Thanks, RG

R G
  • 9
  • 1

0 Answers0