學習 C 編程語言并在打開 Xcode 13.2 和我面前的命令列工具專案的情況下閱讀檔案。
在宣告/陣列/可變長度陣列部分閱讀此內容:
{
int n = 1;
label:
int a[n]; // re-allocated 10 times, each with a different size
printf("The array has %zu elements\n", sizeof a / sizeof *a);
if (n < 10) goto label; // leaving the scope of a VLA ends its lifetime
}
并在函式內部的 Xcode 中復制它main,它只會在該行旁邊給我“預期運算式”錯誤int a[n];。我試圖把它放到一個單獨的函式中,但這不是解決方案。
這里出了什么問題?
謝謝
uj5u.com熱心網友回復:
唯一可以跟在標簽后面的是陳述句,而宣告不是陳述句。您必須以某種方式將標簽后面的代碼包裝在一個塊中:
#include <stdio.h>
int main( void )
{
int n = 1;
label:
do {
int a[n];
printf( "The array has %zu elements\n", sizeof a / sizeof a[0] );
if ( n < 10 ) goto label;
} while ( 0 );
return 0;
}
現在結果應該是你所期望的:
$ ./vla
The array has 1 elements
The array has 2 elements
The array has 3 elements
The array has 4 elements
The array has 5 elements
The array has 6 elements
The array has 7 elements
The array has 8 elements
The array has 9 elements
看在上帝的份上,不要這樣做。
編輯
在標簽后只使用一個空陳述句:
#include <stdio.h>
int main( void )
{
int n = 1;
label:
;
int a[n];
printf( "The array has %zu elements\n", sizeof a / sizeof a[0] );
if ( n < 10 ) goto label;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414897.html
標籤:
