如何分配一个C结构内嵌? [英] How to assign a C struct inline?

查看:64
本文介绍了如何分配一个C结构内嵌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct {
    int hour;
    int min;
    int sec;
} counter_t;

和在code,我想初始化该结构的情况下,没有明确初始化每个成员变量。也就是说,我愿意做这样的事情:

And in the code, I'd like to initialize instances of this struct without explicitly initializing each member variable. That is, I'd like to do something like:

counter_t counter;
counter = {10,30,47}; //doesn't work

有关十时30分47秒

而不是

counter.hour = 10;
counter.min = 30;
counter.sec = 47;

不记得语法对于这一点,并没有马上找到一种方法,从谷歌搜索做到这一点。

Don't recall syntax for this, and didn't immediately find a way to do this from Googling.

谢谢!

推荐答案

初始化:

counter_t c = {10, 30, 47};

分配:

c = (counter_t){10, 30, 48};

后者被称为复合文字。

The latter is called a "compound literal".

这篇关于如何分配一个C结构内嵌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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