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

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

问题描述

考虑一个C结构:

struct T {
    int x;
    int y;
};

在这部分初始化为

struct T t = {42};

t.y 的保证是0或这是编译器的实现决定?

is t.y guaranteed to be 0 or is this an implementation decision of the compiler?

推荐答案

标准草案项目8.5.1.7:

item 8.5.1.7 of standard draft:

-7-如果列表中的初始化更少比有成员在
  骨料,然后每个成员不
  明确的初始化应
  默认初始化(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" };


  
  

初​​始化ss.a 1,ss.b与
  ASDF,并与ss.c的价值
  前$形式INT的对$ pssion(),即,
  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天全站免登陆