日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言中for中声明变量,C中for循环中的变量重新声明

發布時間:2024/9/30 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言中for中声明变量,C中for循环中的变量重新声明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當嘗試為多個平臺編譯以下(簡化)代碼時,我發現它在某些方面是失敗的,即IBM的xlC_r.進一步的調查發現,它也沒有進來和cl..它與g和Solaris的CC成功編譯.

這是代碼:

int main()

{

int a1[1];

bool a2[1];

for (int *it = a1, *end = a1+1; it != end; ++it) {

//...

bool *jt = a2, *end = a2+1;

//...

}

}

xlC_r錯誤:

"main.cpp", line 8.25: 1540-0400 (S) "end" has a conflicting declaration.

"main.cpp", line 6.25: 1540-0425 (I) "end" is defined on line 6 of "main.cpp".

ang子錯誤:

main.cpp:8:25: error: redefinition of 'end' with a different type

bool *jt = a2, *end = a2+1;

^

main.cpp:6:25: note: previous definition is here

for (int *it = a1, *end = a1+1; it != end; ++it) {

^

來錯誤:

"ComeauTest.c", line 8: error: "end", declared in for-loop initialization, may not

be redeclared in this scope

bool *jt = a2, *end = a2+1;

^

問題是為什么這是一個錯誤?

看看2003年的標準,它說如下(6.5.3):

The for statement

for ( for-init-statement; condition; expression ) statement

is equivalent to

{

for-init-statement;

while ( condition ) {

statement;

expression;

}

}

except that names declared in the for-init-statement are in the same

declarative-region as those declared in condition

這里沒有聲明的名稱.

另外,它說(6.5.1):

When the condition of a while statement is a declaration, the scope

of the variable that is declared extends from its point of declaration

(3.3.1) to the end of the while statement. A while statement of the form

while (T t = x) statement

is equivalent to

label:

{

T t = x;

if (t) {

statement;

goto label;

}

}

再次,我不知道這是相關的,因為沒有聲明在條件.所以給出6.5.3的等效重寫,我的代碼應該是一樣的:

int main()

{

int a1[1];

bool a2[1];

{

int *it = a1, *end = a1+1;

while (it != end) {

//...

bool *jt = a2, *end = a2+1;

//...

++it;

}

}

}

這顯然會允許結束重新聲明.

總結

以上是生活随笔為你收集整理的c语言中for中声明变量,C中for循环中的变量重新声明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。