未由指定初始化程序命名的字段会发生什么? [英] What happens to fields not named by a designated initializer?

查看:15
本文介绍了未由指定初始化程序命名的字段会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C99(而不是 C++)中,可以使用以下语法初始化结构:

In C99 (and not in C++), it's possible to initialize structs using this syntax:

struct info
{
    char    name[8+1];
    int     sz;
    int     typ;
};

struct info  arr[] =
{
    [0] = { .sz = 20, .name = "abc" },
    [9] = { .sz = -1, .name = "" }
};

未指定的字段会发生什么?

What happens to the unspecified fields?

推荐答案

它们归零.来自 C99 标准§6.7.8 (Initialization)/21,

They are zeroed. From the C99 standard §6.7.8 (Initialization)/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.

这篇关于未由指定初始化程序命名的字段会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆