小编Sau*_*rma的帖子

数组维度中的const值

下面的代码片段在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)

有人可以解释为什么它不会在主要内部抛出错误.

c const

2
推荐指数
1
解决办法
57
查看次数

包含const_cast的代码片段的说明

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 …

c++ c++11

0
推荐指数
1
解决办法
83
查看次数

标签 统计

c ×1

c++ ×1

c++11 ×1

const ×1