找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 336|回复: 1

Question11.8:I don't understand why I can't ue const values in intializers and array dientsi

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-17 20:51:44 | 显示全部楼层 |阅读模式
Q: I don't understand why I can't use const values in initializers and array dimensions, as in
                        const int n = 5;
                        int a[n];
A: The const qualifier really means ``read-only''; an object so qualified is a run-time object which cannot (normally) be assigned to. The value of
a const-qualified object is therefore not a constant expression in the full sense of the term, and cannot be used for array dimensions, case labels, and the like. (C is unlike C++ in this regard.) When you need a true compile-time constant, use a preprocessor #define (or perhaps an enum).

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-17 21:02:06 | 显示全部楼层
问题:我不明白为什么我不能使用初始化的const值在数组维数中国,像这样:
           const int n = 5;
                    int a[n];
回答:const限定语的意味着“只读”;一个被这样限定的对象是不能在运行过程中被重新赋值的(正常的赋值)。被const限定的对象的值并不是完整意义上的常量,不能用作数组维数,case行标等类似的情况(在这一点上C和C++不同)。当你需要使用一个真正在编译期间的常量的时候,使用预处理命令#define(或者enum)。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-2 04:20 , Processed in 0.093201 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表