我们可以在数组内联声明中分配更少的数组内容吗?

标签 c arrays initialization language-lawyer

我得到了一个数组A,它封装了类型为BM 数组。每个这样的 B 数组都有 3-components。 但是,我需要将我的条目填充到 4 个组件。一种方法是将我的值修改为具有第 4 个零分量:

uint16_t A[M][4] = {{a0,a1,a2,0}, {a3,a4,a5,0}, ... , {aM-1,aM,aM+1,0}};

但是,我意识到我可以如下声明 A 并且 clang 没有提示:

uint16_t A[M][4] = {{a0,a1,a2}, {a3,a4,a5}, ... , {aM-1,aM,aM+1}};

这合法吗?编译器会扩展 A 的条目以包含第 4 个组件吗?能否请您指出 C 标准 (PEP) 我在哪里可以看到这种行为的解释。

最佳答案

是的,这应该是有效的。

引用 C11,章节 §6.7.9,初始化

Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, [...]

然后,(强调我的)

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

关于我们可以在数组内联声明中分配更少的数组内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34884824/

相关文章:

C getaddrinfo() 无法解析名称

c - 尝试将字符串转换为整数数组的艰难时期

c++ - gnu 在目录名 vpath 中留出空格

c - 嵌入式 C 语言中的宏如何影响内存?

python / NumPy : all values in array up to x?

arrays - 将每一行重复成新的一行

c++ - new[] 是否初始化一个内置数组?

c++ - 我应该如何初始化并正确使用 `struct` ?

java - 如果在主方法中声明并在循环内进行初始化,那么从 For 循环外部访问变量是否不可能在 Java 中?

c - 如何生成 16 位查找表来计算设置位