1

I wonder if there is a way to initialize a member of a member of a struct. Let`s say I have 2 structs, t1 and t2. t2 contains 2 instances of t1. like this:

typedef struct t1{

    int                     num;

} t1;

typedef struct t2{

    t1                      sub_one;
    t1                      sub_two;
    int                     num;

} t2;

Is it possible to declare sub_one.num to a number and sub_two.num to a different number at the time I iniatilize t2. something like:

typedef struct t2{

    t1                      sub_one.num = 1;
    t1                      sub_two.num = 2;
    int                     num;

} t2;

is that possible?

  • A related one: [Why can't we initialize members inside a structure?](http://stackoverflow.com/q/225089/2157640) I think that what you want is impossible and answers to that question try to explain why. [Kerrek SB’ answer](http://stackoverflow.com/a/24601366/2157640) is the best you can get, IMO. – Palec Jul 07 '14 at 00:24

1 Answers1

3

How about the usual brace syntax:

t2 x = { { 1 }, { 2 }, 25 };
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084