未初始化的结构体成员总是设置为零? [英] Are uninitialized struct members always set to zero?

查看:901
本文介绍了未初始化的结构体成员总是设置为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个C结构:

struct T {
    int x;
    int y;
};

部分初始化为

struct T t = {42};

ty 保证为0,编译器

推荐答案

标准草案的项目8.5.1.7:

item 8.5.1.7 of standard draft:


-7-如果列表中的初始化器比
聚合中的成员少,那么每个非
初始化的成员应该是
default-initialized dcl.init)。
[例如:

-7- If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized (dcl.init). [Example:

struct S { int a; char* b; int c; };
S ss = { 1, "asdf" };

使用1初始化ss.a,使用
asdf初始化ss.b,ss .c与int()形式的
表达式的值,即
0.]

initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is, 0. ]

这篇关于未初始化的结构体成员总是设置为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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