#include <iostream>
using namespace std;
int main()
{
int a = 10;
const int constA = a;
//const int constA = 10;
int *p = (int*)&constA;
*p = 100;
cout << "constA=" << constA << endl;
cout << "*p=" << *p << endl;
system("pause");
return 0;
}
1.c++局部const常量是放在“符号表”中的,不分配内存,对其取地址时,才会分配内存
所以
如果上述代码中是const int constA = 10;则输出结果为:
constA=10
*p=100
2.这里的解释就是int a = 10;其中a 已经分配内存(放在哪个区来着,栈区?!),且通过constA的地址间接操作ConstA的内存,变成了100,当然a还是等于10
如果上述代码中是const int constA = a;则输出结果为:
constA=100
*p=100