简体   繁体   English

结构数组,指定初始化程序含义

[英]Array of structure, designated initializer implication

I already know that: 我已经知道了:

1) array "partial initialization" 1)数组“部分初始化”

int array[10] = {1};

results in initialising array[0] with value 1, and all other array values with 0. 导致初始化数组[0]值为1,所有其他数组值为0。

2) designated initializers 2)指定的初始化器

struct metadata {
    int val0;
    int val1;
    char *name;
};

void func(void){
    struct metadata my_metadata = { .val0 = 1 };

Results in initialising my_metadata.val0 with 1, my_metadata.val1 with 0 and my_metadata.name with NULL. 导致my_metadata.val0初始化为1,my_metadata.val1为0,my_metadata.name为NULL。

3) Here comes the question: 3)问题出现了:

struct metadata {
    int val0;
    int val1;
    char *name;
};

void func(void){
    struct metadata my_metadata_array[2] = { {.val0 = 1} };

What will be the resulting operation of this ? 这将产生什么样的操作?

Bonus question 1: could someone point me to the reference documentation for "partial initialisation" (I do not know if it is a compiler extension or part of a specific standard version) ? 奖金问题1:有人可以指向“部分初始化”的参考文档(我不知道它是编译器扩展还是特定标准版本的一部分)?

Bonus question 2: same than bonus question 1 for "designated initializers". 奖金问题2:与“指定初始化者”的奖金问题1相同。

Just like in "partial initialization", the first metadata element will be initialized with val0 element set to 1, everything else in that element and the second element will be default initialized (or the same as objects with static duration). 就像在“部分初始化”中一样,第一个metadata元素将被初始化,其中val0元素设置为1,该元素中的所有其他元素和第二个元素将被默认初始化(或者与具有静态持续时间的对象相同)。

Bonus 1: It has been part of the standard for a while 奖金1:它已经成为标准的一部分了一段时间

C Standard 6.7.9-21: C标准6.7.9-21:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration. 如果括号括起的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小的数组的字符串文字中的字符数少于数组中的元素,则聚合的其余部分应为隐式初始化与具有静态存储持续时间的对象相同。

Bonus 2: It is part of C99 奖金2:它是C99的一部分

C Standard 6.7.9-7 C标准6.7.9-7

If a designator has the form . 如果指定者有表格。 identifier then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type. 标识符然后当前对象(下面定义)应具有结构或联合类型,标识符应该是该类型成员的名称。

Yes (you got the way of thinking fine), that's the result, it will set that variable to 1 and default initialize the rest of them. 是的(你的思路很好),这就是结果,它会将该变量设置为1并默认初始化其余部分。

Example: 例:

#include <stdio.h>

struct metadata {
    int val0;
    int val1;
    char *name;
};

int main(void){
    struct metadata my_metadata_array[2] = { {.val0 = 1} };
    printf("%d %d %s\n", my_metadata_array[0].val0,  my_metadata_array[0].val1,  my_metadata_array[0].name);
    printf("%d %d %s\n", my_metadata_array[1].val0,  my_metadata_array[1].val1,  my_metadata_array[1].name);
    return 0;
}

Output: 输出:

1 0 (null)
0 0 (null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM