Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Currently, I have a struct in a union. For example,

Struct foo{
    Union u{
         Struct s1{
             int i1;
         } ss1;
         Struct s2{
             int i2;
         } ss2;
    } wrap;
};

So when I want to initialize the union, I tried to do like this.

foo f = {};
f.u.ss1 = {
    .i1 = 0;
}

But the error shows no match for operator = (operand types and braced-enclosed initializer list).

So what is the right way to do the initialize? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

The initialisation should be:

foo f;
f.wrap.ss1 = {0 /*, comma seperated values, */};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...