How to use a struct inside another struct?

2019-04-12 07:53发布

问题:

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;
};

So in the main when I come to use it :

int main() {
    A stage;
    stage.B.number;
}

Is that right if not how do i Use it??

回答1:

Each member variable of a struct generally has a name and a type. In your code, the first member of A has type int and name data. The second member only has a type. You need to give it a name. Let's say b:

struct A {
  int data;
  B b;
};

To do that, the compiler needs to already know what B is, so declare that struct before you declare A.

To access a nested member, refer to each member along the path by name, separated by .:

A stage;
stage.b.number = 5;


回答2:

struct A {
    struct B {
       int number;
    };
    B b;
    int data;
};
int main() {
    A a;
    a.b.number;
    a.data;
}


回答3:

struct B {  // <-- declare before
  int number;
};
struct A {
 int data;
 B b; // <--- declare data member of `B`
 };

Now you can use it as,

stage.b.number;


回答4:

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;


回答5:

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

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


回答6:

struct TestStruct {
    short Var1;
    float Var2;
    char Var3;

    struct TestStruct2 {
        char myType;
        CString myTitle;
        TestStruct2(char b1,CString b2):myType(b1), myTitle(b2){}
    };

    std::vector<TestStruct2> testStruct2;

    TestStruct(short a1,float a2,char a3): Var1(a1), Var2(a2), Var3(a3) {
        testStruct2.push_back(TestStruct2(0,"Test Title"));
        testStruct2.push_back(TestStruct2(4,"Test2 Title"));
    }       
};
std::vector<TestStruct> testStruct;

//push smthng to vec later and call 
testStruct.push_back(TestStruct(10,55.5,100));
TRACE("myTest:%s\n",testStruct[0].testStruct2[1].myTitle);


回答7:

I have the somewhat like the following code running for a while live now and it works.

//define a timer
struct lightTimer {
  unsigned long time;                                                 //time in seconds since midnight so range is 0-86400
  byte          percentage;                                           // in percentage so range is 0-100
};

//define a list of timers
struct lightTable {
  lightTimer timer[50];
  int        otherVar;
};

//and make 5 instances
struct lightTable channel[5];                           //all channels are now memory allocated

@zx485: EDIT: Edited/cleaned the code. Excuse for the raw dump.

Explanation:

Define a lightTimer. Basically a struct that contains 2 vars.

struct lightTimer {

Define a lightTable. First element is a lightTimer.

struct lightTable {

Make an actual (named) instance:

struct lightTable channel[5];

We now have 5 channels with 50 timers.

Access like:

channel[5].timer[10].time = 86400;
channel[5].timer[10].percentage = 50;
channel[2].otherVar = 50000;


标签: c++ struct