About default C struct values, what about this code?

c, struct

Solution

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};

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

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

Running example @ ideone : http://ideone.com/1QqCB

The original line

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

is a syntax error in C.

Problem

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... }; ``` and looking about, I found this (C++) code: ``` struct a{ a() : i(0), j(0) {}; INT i; INT j;} ``` I never saw anything like this for C. Please, help me to understand it; I think that it is very nice! 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++...

Original source

Related problems