How to use a struct inside another struct?

前端 未结 7 1331
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 21:05

I want to use a nested structure but i dont know how to enter data in it, for example:

struct A {
    int data;
    struct B;
};
struct B {
    int number;
};
         


        
7条回答
  •  逝去的感伤
    2021-02-05 21:18

    The struct B within A must have a name of some sort so you can reference it:

    struct B {
        int number;
    };
    struct A {
        int data;
        struct B myB;
    };
    :
    struct A myA;
    myA.myB.number = 42;
    

提交回复
热议问题