How to use a struct inside another struct?

前端 未结 7 1342
佛祖请我去吃肉
佛祖请我去吃肉 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:29

    struct A 
    {
      int data;
      struct B
      {
        int number;
      }b;
    };
    
    int main()
    {
      A stage = { 42, {100} };
      assert(stage.data == 42);
      assert(stage.b.number == 100);   
    }
    

提交回复
热议问题