我得到了一个数组A
,它封装了类型为B
的M
数组。每个这样的 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/