我指的是这本书 "Theory and Problems of Programming with C" Gottfried 着(Schaum 的大纲系列,第 2 版,1996 年)。 在关于数组的第 9 章第 243 页第 9.1 节中,它说:
Automatic arrays, unlike automatic variables, cannot be initialized. However, external and static array definitions can include the assignment of initial values if desired.
我不明白这条突出显示的声明的含义。我尝试在函数内部初始化数组(使用和不使用 auto
关键字),但没有发现任何问题。
void func1 (void)
{
auto int array1[5] ={1,0,4,1,5};
charVar1='M';
printf("%d", *(array1+4));
}
最佳答案
回答第一部分
Automatic arrays, unlike automatic variables, cannot be initialized
假设“自动数组”实际上是“长度在运行时确定的自动存储持续时间的数组数据结构”
是的,这里指的是一个variable length array .简单逻辑不能初始化,大小在运行时确定。
引用 C11
标准,第 6.7.9 章,初始化(强调我的)
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.
否则,对于没有任何存储类说明符的局部变量,默认为 auto
和非 VLA 类型的自动数组,可以 已初始化,当然。
关于我们不能初始化自动数组变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33020791/