我发现了一个关于c ++的问题 代码:
volatile const int a = 10;
int *p = const_cast<int*>(&a);
*p = 2;
cout << "value a="<< a << endl;
cout << "value *p=" <<*p << endl;
cout << "address a=" <<&a << endl; //output 1!!
cout << "address p=" <<p << endl;
谁能告诉我为什么a的地址是1?
答案 0 :(得分:1)
代码的行为是未定义。
Evalutation | # of places<br>
===============================<br>
Bad | # of places with less than 40% "good" scores <br>
Good | # of places with more than 40%, less than 70% "good" scores <br>
Excellent | # of places with more than 70% "good" scores <br>
最初声明为a
。
您尝试通过已放弃const
的指针来更改其值。该语言不允许这样做。
const
在这种情况下是红鲱鱼。