亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3156 | 回復(fù): 6
打印 上一主題 下一主題

還是關(guān)于全局變量初始化的問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-09-25 12:23 |只看該作者 |倒序?yàn)g覽
參考http://www.72891.cn/thread-1275329-5-1.html的討論
c99中規(guī)定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條語句的初始化就應(yīng)該合法了
但事實(shí)上仍然不能通過編譯

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2008-09-25 12:33 |只看該作者
標(biāo)準(zhǔn)中的const不是讓你加上const修飾

標(biāo)準(zhǔn)的意思是,如果你要用一個值來初始化某個static變量,那么這個初始值在編譯時應(yīng)該是一個有確定值常數(shù)。
&i表示變量的地址,這在編譯時是可以確定的。p表示變量p的值,這是在編譯的時候無法確定的。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2008-09-25 12:34 |只看該作者
在全局變量初始化時不要賦值為一個變量。&i是地址是一個常量,p是一個變量。

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2008-09-25 12:40 |只看該作者

回復(fù) #2 prc 的帖子

如果按照你值能不能確定的理論的理論來說的話
第3條語句的右值也是可以確定的,跟2一樣的

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

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2008-09-25 13:20 |只看該作者
原帖由 lemoncookie 于 2008-9-25 12:40 發(fā)表
如果按照你值能不能確定的理論的理論來說的話
第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
所以編譯器可以認(rèn)為這就是一個錯誤

[ 本帖最后由 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 [報(bào)告]
發(fā)表于 2008-09-25 13:49 |只看該作者
原帖由 prc 于 2008-9-25 13:20 發(fā)表


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
所有編 ...

學(xué)習(xí): 需要好好理解一下constant expressions

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2008-09-25 14:41 |只看該作者

回復(fù) #6 ynchnluiti 的帖子

ok,結(jié)帖吧

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.
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP