Shadi
Shadi

Reputation: 481

How to use a struct inside another struct?

I want to use a nested structure, but I don't know how to enter data in it. For example:

struct A {
    int data;
    struct B;
};

struct B {
    int number;
};

So in main() when I come to use it:

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

Is that right? If not how do I use it?

Upvotes: 17

Views: 168227

Answers (8)

Brijesh B Naik
Brijesh B Naik

Reputation: 1

#include <iostream>


typedef struct 
{
       typedef struct 
       {
         int x, y, z;  
       }poi;
       poi pi;
      
}point;

    
int main(){
    
    
    point pt;
    pt.pi.x= 5;
    std::cout << pt.pi.x << std::endl;
}

Upvotes: 0

cellie
cellie

Reputation: 1

I have 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;

Upvotes: 0

Erkki Pilving
Erkki Pilving

Reputation: 1

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

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881563

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;

Upvotes: 9

greatwolf
greatwolf

Reputation: 20838

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

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

Upvotes: 2

iammilind
iammilind

Reputation: 69988

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;

Upvotes: 11

Rob Kennedy
Rob Kennedy

Reputation: 163287

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;

Upvotes: 21

Puppy
Puppy

Reputation: 146940

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

Upvotes: 18

Related Questions