Mahi Mahi - 17 days ago 5
C Question

Structure usage inside another structure

Please check the code snippet given below. Pardon me if anything is wrong with the syntax or anything else. I am a newbie trying to learn structure usage in detail and I happened to come across the following sample. I just wanted to know how to access Stu_Details from Class_Details

typedef struct {
int age;
int RollNo;
int Rank;
char Name[10];
}TypeStudent;

typedef struct {
char class_name[20];
TypeStudent Students[20];
}TypeClass;

const TypeStudent Stu_Details[] = {
{ 3, 1, 18, "Mahesh"},
{ 3, 1, 7, "Kumar"}
};

const TypeClass Class_Details[]= {
{ "Class 10", Stu_Details}, //two students details
{ "Class 8", 0} //no student details attached
};

Answer

This one worked for me.

typedef struct {
    int age;
    int RollNo;
    int Rank;
    char Name[10];
}TypeStudent;

typedef struct {
    char class_name[20];
    TypeStudent *Students;
}TypeClass;

int main()
{

   const TypeStudent  Stu_Details[] = {
     { 3,   1,  18, "Mahesh"},
     { 3,   1,   7,  "Kumar"}
   };

   const TypeClass Class_Details[]= {
     { "Class 10",     Stu_Details},  //two students details
     { "Class 8",                0}   //no student details attached
   };

   printf("\r\nTest: %s",Class_Details[0].Students->Name);
   return 0;
}