[C]變長陣列
阿新 • • 發佈:2018-11-27
變長陣列在C99及C11的標準中支援,嚴格講在C++的所有標準中都不支援變長陣列,只是各家編譯器對語言的擴充套件
//t.c
#include<stdio.h>
int foo(int n){
int x[n];
printf("%lu\n",sizeof(x));
return n;
}
int main()
{
foo(10);
foo(20);
return 0;
}
嚴格按照C99標準編譯: clang t.c -o t -std=c99 -pedantic,輸出正常
嚴格按照C11標準編譯: clang t.c -o t -std=c11 -pedantic,輸出正常
嚴格按照C89標準編譯: clang t.c -o t -std=c99 -pedantic,提示如下的警告資訊:
t.c:4:7: warning: variable length arrays are a C99 feature [-Wvla-extension]
int x[n];
^
1 warning generated.
//t.cpp #include<stdio.h> int foo(int n){ int x[n]; printf("%lu\n",sizeof(x)); return n; } int main() { foo(10); foo(20); return 0; }
嚴格按照C++98標準:clang t.cpp -o t -std=c++98 -pedantic,提示警告資訊如下:
t.cpp:4:7: warning: variable length arrays are a C99 feature [-Wvla-extension]
int x[n];
^
1 warning generated.
嚴格按照C++11標準:clang t.cpp -o t -std=c++11 -pedantic,提示警告資訊如下:
t.cpp:4:7: warning: variable length arrays are a C99 feature [-Wvla-extension]
int x[n];
^
1 warning generated.