<分区>
给出一组基本的C代码:
int a = 5;
int b[a] = {1,2,3,4,5};
为什么会产生这个错误?
$gcc -o demo demo.c
error: variable-sized object may not be initialized
我明白这是一件坏事,编译器不会允许你这样做。 然而,作为一名教师,我很难解释为什么你不能也不应该这样做。
(假设 C99)
你如何解释这里发生的事情?
标签 c
<分区>
给出一组基本的C代码:
int a = 5;
int b[a] = {1,2,3,4,5};
为什么会产生这个错误?
$gcc -o demo demo.c
error: variable-sized object may not be initialized
我明白这是一件坏事,编译器不会允许你这样做。 然而,作为一名教师,我很难解释为什么你不能也不应该这样做。
(假设 C99)
你如何解释这里发生的事情?
最佳答案
Why does this generate this error?
C 标准不允许这样做。可变长度数组不能使用大括号括起来的初始化列表来初始化。
标准说:
C11-§6.7.9/3:
The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.
关于c - C中的变量列表数组初始化解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37295107/