如何按照C编程语言标准初始化结构 [英] How to initialize a struct in accordance with C programming language standards

查看:151
本文介绍了如何按照C编程语言标准初始化结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要初始化一个struct元素,劈声明和初始化。这是我有:

I want to initialize a struct element, split in declaration and initialization. This is what I have:

typedef struct MY_TYPE {
  boolean flag;
  short int value;
  double stuff;
} MY_TYPE;

void function(void) {
  MY_TYPE a;
  ...
  a = { true, 15, 0.123 }
}

这是声明和初始化的局部变量的方式 MY_TYPE 按照C编程语言标准(C89,C90,C99,C11等)?或者有什么更好的,或者至少工作?

Is this the way to declare and initialize a local variable of MY_TYPE in accordance with C programming language standards (C89, C90, C99, C11, etc.)? Or is there anything better or at least working?

更新我最后不得不在这里我根据我的需要设置每一个子元素静态初始化元素。

Update I ended up having a static initialization element where I set every subelement according to my needs.

推荐答案

在(ANSI)C99,可以使用指定初始化初始化结构:

In (ANSI) C99, you can use a designated initializer to initialize a structure:

MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 };

编辑:其他成员被初始化为零:已失字段成员隐式初始化一样具有静态存储持续时间的对象。 (<一href=\"https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html\">https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html)

这篇关于如何按照C编程语言标准初始化结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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