我们不能初始化自动数组变量吗?

标签 c

我指的是这本书 "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));
}

添加页面图像enter image description here

最佳答案

回答第一部分

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/

相关文章:

c - 使用 pid linux 制作一个显示 CPU(%) 和内存使用情况(kB) 的程序

c - Linux "ls -al"类似 C 语言程序

打印的 c 部分在 linux 上不工作

c++ - 将 int 转换为 void 指针并再次返回 int 是否安全?

c - 与体系结构无关的 C 代码

c - 将数据存储在列表 c 中

无法从 'double' 转换为 'double (*)'

c - 需要创建一个删除列表中最后一个学生信息的函数

c++ - 用于检查 c/c++ 源代码中注释拼写的 Linux 工具

C - 将 kbhit 中的 key 写入文件会使程序崩溃