c - 在多维数组中将聚合初始值设定项与标量初始值设定项混合

标签 c multidimensional-array

这可能吗?如果有人能指导我在标准中提到这一点,我会很高兴。

对我来说,它的工作方式与我预期的一样,使用 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 and w[1].a[0] is 2; all the other elements are zero

关于c - 在多维数组中将聚合初始值设定项与标量初始值设定项混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933023/

相关文章:

javascript - php多维数组到javascript无法通过循环访问

python - 如何根据python中的第一行字母将多维列表按字母顺序排序

c - 如何通过保持其他单元格值不变来获取二维数组的一个单元格中的值,然后如何应用 Qsort?

c++ - 新建一个打开的窗口并插入图片

c++ - 确保创建线程并在广播前等待

c - 树排序不排序我的数组

c - 使用结构和 malloc 时为 "error: conversion to non-scalar type requested"

c - Printf 的变体及其原型(prototype)理解

java - 二维数组上 ArrayIndexOutOfBoundsException 的快速帮助

c# - 在单个循环中循环多维数组