当C中的struct数组有部分初始化时会发生什么? [英] What happens when there is partial initialization of an array of struct in C?

查看:375
本文介绍了当C中的struct数组有部分初始化时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码是什么意思?

struct foo_t {
    int a;
    int b;
} foo[10] = {{0,0}}

foo [0]是 {0,0} ,但其余的呢?
C标准如何处理这个问题?

foo[0] is {0,0}, but what about the rest? How does the C standard handle this?

ADDED。我创建了一个详尽的答案在这里

推荐答案

整个数组将被初始化为structs,值为0 a b 。这类似于以下具有原始值的情况:

The entire array will be initialized with structs with the value 0 for both a and b. This is similar to the following case with a primitive value:

int foo[10] = {0};

其中数组中的每个整数将使用 0

Where every integer in the array will be initialized with the value 0.

C99标准规定如下:

The C99 standard specifies the following:


如果在括号括起来的列表中的初始化方法少于聚合的元素或成员
,或者字符串文字中用于初始化已知
大小的数组的字符少于数组,聚合的剩余部分应该是
,默认初始化为与具有静态存储持续时间的对象相同。

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.

这篇关于当C中的struct数组有部分初始化时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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