免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3113 | 回复: 6
打印 上一主题 下一主题

还是关于全局变量初始化的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-25 12:23 |只看该作者 |倒序浏览
参考http://bbs.chinaunix.net/thread-1275329-5-1.html的讨论
c99中规定All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
1 int i;
2 int *p = &i;
3 int *q = p;


上面代码中第3行是不符合初始化要求的,那么如果把第2行改成
2 int * const p = &i;


似乎第3条语句的初始化就应该合法了
但事实上仍然不能通过编译

论坛徽章:
0
2 [报告]
发表于 2008-09-25 12:33 |只看该作者
标准中的const不是让你加上const修饰

标准的意思是,如果你要用一个值来初始化某个static变量,那么这个初始值在编译时应该是一个有确定值常数。
&i表示变量的地址,这在编译时是可以确定的。p表示变量p的值,这是在编译的时候无法确定的。

论坛徽章:
0
3 [报告]
发表于 2008-09-25 12:34 |只看该作者
在全局变量初始化时不要赋值为一个变量。&i是地址是一个常量,p是一个变量。

论坛徽章:
0
4 [报告]
发表于 2008-09-25 12:40 |只看该作者

回复 #2 prc 的帖子

如果按照你值能不能确定的理论的理论来说的话
第3条语句的右值也是可以确定的,跟2一样的

[ 本帖最后由 lemoncookie 于 2008-9-25 12:56 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-09-25 13:20 |只看该作者
原帖由 lemoncookie 于 2008-9-25 12:40 发表
如果按照你值能不能确定的理论的理论来说的话
第3条语句的右值也是可以确定的,跟2一样的


All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

p既不是const expression
也不是string literal
所以编译器可以认为这就是一个错误

[ 本帖最后由 prc 于 2008-9-25 13:55 编辑 ]

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
6 [报告]
发表于 2008-09-25 13:49 |只看该作者
原帖由 prc 于 2008-9-25 13:20 发表


All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

p既不是const expression
也不是string literal
所有编 ...

学习: 需要好好理解一下constant expressions

论坛徽章:
0
7 [报告]
发表于 2008-09-25 14:41 |只看该作者

回复 #6 ynchnluiti 的帖子

ok,结帖吧

A constant expression can be used any place a constant can be used. The syntax
is as follows:
constant-expression:              conditional-expression
A constant expression cannot contain assignment, increment, decrement,
function-call, or comma operators. It must evaluate to a constant that is in the
range of representable values for its type. Otherwise, the semantic rules for the
evaluation of nonconstant expressions apply.
Constant expressions are separated into three classes:
• An integral constant expression has integral type and is restricted to
    operands that are integral constants, sizeof expressions (whose operands
    do not have variable length array type or a parenthesized name of such a
    type), and floating constants that are the immediate operands of integral
    casts.
• An arithmetic constant expression has arithmetic type and is restricted to
    operands that are arithmetic constants, and sizeof expressions (whose
    operands do not have variable length array type or a parenthesized name of
    such a type). Cast expressions in arithmetic constant expressions can convert
    only between arithmetic types.
• An address constant is a pointer to an lvalue designating an object of static
    storage duration, or a pointer to a function designator. It can be created
    explicitly or implicitly, as long as no attempt is made to access an object
    value.
Either address or arithmetic constant expressions can be used in initializers. In
addition, initializers can contain null pointer constants and address constants
(for object types), and plus or minus integral constant expressions.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP