关于默认的C结构值,你看这个code? [英] About default C struct values, what about this code?

查看:99
本文介绍了关于默认的C结构值,你看这个code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建具有默认值结构。我不知道如何做到这一点,因为每个code,我看到,大约是初始化,我会为它的自然的方式一样...

I'm trying to create structs with default values. I don't know how to accomplish this because every code that I see, is about initialising, and I would it for the natural way like...

struct stuff {
  int stuff_a = 1;
  int stuff_b = 2...
  ...and so on...
};

和细算一下,我发现这个(C ++)code:

and looking about, I found this (C++) code:

struct a{   a() : i(0), j(0) {};  INT i;   INT j;}

我从来没有见过这样的事为C.请帮助我理解它;我觉得这是非常好的!

I never saw anything like this for C. Please, help me to understand it; I think that it is very nice!

更新:等一下,我在问关于C !!!!为什么改变了我的问题吗?如果在C是不可能只是说...我不知道C ++,我不知道这是关于C ++ ...

UPDATE: Wait, I'm asking about C!!!! Why changed my question? If that is not possible in C just say... I don't know C++, I didn't know that was about C++...

推荐答案

如果您想设置一个struct对象一气呵成你有C99编译,试试这个:

If you want to set a struct object in one go and you have a C99 compiler, try this:

struct stuff {
    int stuff_a;
    int stuff_b;
    // and so on...
};

struct stuff foo;
/* ... code ... */
foo = (struct stuff){.stuff_b = 42, .stuff_a = -1000};

另外,用C89编译器,你必须通过一个每个成员来设置:

Otherwise, with a C89 compiler, you have to set each member one by one:

foo.stuff_b = 42;
foo.stuff_a = -1000;


运行例如@ ideone: http://ideone.com/1QqCB

原线

struct a{   a() : i(0), j(0) {}   INT i;   INT j;}

在C语法错误。

这篇关于关于默认的C结构值,你看这个code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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