12

How can I initialize this nested struct in C?

typedef struct _s0 {
   int size;
   double * elems;
}StructInner ;

typedef struct _s1 {
   StructInner a, b, c, d, e;
   long f;
   char[16] s;
}StructOuter;  StructOuter myvar = {/* what ? */ };
2
  • 5
    should be char s[16];, not char[16] s; Commented Nov 19, 2009 at 8:56
  • There's a similar post here on SO for C++. Commented Nov 19, 2009 at 8:57

4 Answers 4

20

To initialize everything to 0 (of the right kind)

StructOuter myvar = {0};

To initialize the members to a specific value

StructOuter myvar = {{0, NULL}, {0, NULL}, {0, NULL},
                     {0, NULL}, {0, NULL}, 42.0, "foo"};
/* that's {a, b, c, d, e, f, s} */
/* where each of a, b, c, d, e is {size, elems} */

Edit

If you have a C99 compiler, you can also use "designated initializers", as in:

StructOuter myvar = {.c = {1000, NULL}, .f = 42.0, .s = "foo"};
/* c, f, and s initialized to specific values */
/* a, b, d, and e will be initialized to 0 (of the right kind) */
Sign up to request clarification or add additional context in comments.

2 Comments

Just to clarify, StructOuter myvar = { 0 }; will do the same (0-initialization) for all inner structs, so we don't need to explicitly set those to {0, NULL}, right?
Yes @domsson, the { 0 } initializer will 0-initialize everything, recursively when needed.
5

To highlight struct labels in particular:

StructInner a = {
    .size: 1,
    .elems: { 1.0, 2.0 }, /* optional comma */
};

StructOuter b = {
    .a = a, /* struct labels start with a dot */
    .b = a,
         a, /* they are optional and you can mix-and-match */
         a,
    .e = {  /* nested struct initialization */
        .size: 1,
        .elems: a.elems
    },
    .f = 1.0,
    .s = "Hello", /* optional comma */
};

Comments

2
double a[] = { 1.0, 2.0 };
double b[] = { 1.0, 2.0, 3.0 };
StructOuter myvar = { { 2, a }, { 3, b }, { 2, a }, { 3, b }, { 2, a }, 1, "a" };

It seems a and b cannot be initialized in-place in plain C

Comments

1

Following also works with GCC, C99. GCC doesnt complain about it. I am not sure if this is standard.

double arr[] = { 1.0, 2.0 };   // should be static or global
StructOuter myvar = 
{
    .f = 42,
    .s = "foo",
    .a.size = 2,
    .a.elems = &arr,
    .b.size = 0,  // you can explicitly show that it is zero
    // missing members will actually be initialized to zero
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.