简体   繁体   中英

How to use a struct inside another struct?

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??

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;
struct A {
    struct B {
       int number;
    };
    B b;
    int data;
};
int main() {
    A a;
    a.b.number;
    a.data;
}
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;

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;
struct A 
{
  int data;
  struct B
  {
    int number;
  }b;
};

int main()
{
  A stage = { 42, {100} };
  assert(stage.data == 42);
  assert(stage.b.number == 100);   
}
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);

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM