Chinaunix首页 | 论坛 | 博客
  • 博客访问: 700903
  • 博文数量: 126
  • 博客积分: 2944
  • 博客等级: 上校
  • 技术积分: 1160
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-17 11:09
个人简介

文章分类

全部博文(126)

文章存档

2022年(1)

2018年(1)

2017年(5)

2016年(5)

2013年(5)

2012年(21)

2011年(24)

2010年(1)

2009年(2)

2008年(12)

2007年(6)

2006年(19)

2005年(24)

分类: C/C++

2012-08-08 16:51:36

这个编译错误指没有用常量来初始化变量,比如:

  1. const int LENGTH = 10;
  2. int a[LENGTH];
复杂些的,比如:

  1. typedef struct{
  2.   int i;
  3.   int j;
  4. }sType;

  5. const sType A = {1,2};
  6. const sType B = {3,4};

  7. sType c[2] = {A, B};
究其原因,C语言中的"const"并不表示常量,更确切的意思是“read only".

C99标准 6.7.8 Initialization 第4款:
All the expressions in an initializer for an object that has static storage duration shall be
constant expressions or string literals.

同样的代码,可通过c++编译器,可见C++中“const”的意思名副其实。
阅读(821) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~