下面的代码片段在C中工作和编译
const int n=10;
int main(void)
{
int a[n];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,当在全局范围内声明数组时,它会引发编译错误.
const int n=10;
int a[n];
int main(void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么它不会在主要内部抛出错误.
int main() {
const int i =10;
int *j = const_cast<int*>(&i);
cout<<endl<<"address of i "<<&i;
cout<<endl<<"value of j "<<j;
(*j)++;
cout<<endl<<"value of *j "<<*j;
cout<<endl<<"value of i "<<i;
// If Not use Const //
int k = 10;
int *p = &k;
cout<<endl<<"address of k "<<&i;
cout<<endl<<"address of p "<<&p;
(*p)++;
cout<<endl<<"value of *p "<<*p;
cout<<endl<<"value of k "<<k<<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
地址
i0xbf8267d0
值j0xbf8267d0
值*j11 地址
值i10
地址k0xbf8267d0
地址p0xbf8267c8 …