|
发表于 2021-2-11 12:25:05
|
显示全部楼层
本楼为最佳答案
本帖最后由 jackz007 于 2021-2-11 12:26 编辑
编译程序必须得知道 i 的确切数值才能知道需要为变量 str1 规划、预留出多少个字符的存储空间,同时,也知道需要把多少个存储单元初始化为 '\0',
- #include <stdio.h>
- int main(void)
- {
- int i , n ;
- scanf("%d" , & n) ;
- int d[n] = {0} ;
- for(i = 0 ; i < n ; i ++) scanf("%d" , & d[i]) ;
- printf("%d" , d[0]) ;
- for(i = 1 ; i < n ;i ++) printf("\t%d" , d[i]) ;
- }
复制代码
这个代码用 gcc 10.2.0 编译
- D:\00.Excise\C>gcc -o x x.c
- x.c: In function 'main':
- x.c:7:9: error: variable-sized object may not be initialized
- 7 | int d[n] = {0} ;
- | ^~~
- D:\00.Excise\C>
复制代码
可见,确实无法编译,但是,却可以通过 C++ 编译器的编译
- D:\00.Excise\C>g++ -o x x.c
- D:\00.Excise\C>
- D:\00.Excise\C>x
- 10
- 1 2 3 4 5 6 7 8 9 10
- 1 2 3 4 5 6 7 8 9 10
复制代码
|
|