这可能吗?如果有人能指导我在标准中提到这一点,我会很高兴。
对我来说,它的工作方式与我预期的一样,使用 MinGW
。但是,我想知道这是否标准。这是我测试的代码:
#include <stdio.h>
int main(void) {
int me[3][2] = {{1, 2}, 3, 4, 5, 6};
int x = 0;
while(x<6) printf("%d_", ((int*)me)[x]), ++x;
return 0;
}
输出是:1_2_3_4_5_6_
。
最佳答案
Is that possible? I would be happy if someone can guide me where in the standard this is mentioned.
我认为这是标准的,但我不会说我有确凿的证据。
6.7.9
If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the contained union. Otherwise, only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.
后来,虽然不是专门针对数组,但它给出了一个不一致的括号初始化的例子:
struct { int a[3], b; } w[] = { { 1 }, 2 };
w[0].a[0]
is 1 andw[1].a[0]
is 2; all the other elements are zero
关于c - 在多维数组中将聚合初始值设定项与标量初始值设定项混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933023/