另一个结构内的结构用法

时间:2016-11-21 08:04:46

标签: c structure

请检查下面给出的代码段。如果语法或其他任何内容出错,请原谅我。我是一个新手试图详细了解结构使用情况,我碰巧遇到了以下样本。我只是想知道如何从 Class_Details

访问 Stu_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 
 };  

2 个答案:

答案 0 :(得分:-1)

 Class_Details[0].Students[0].Name 

例如," Mahesh"

答案 1 :(得分:-1)

这个对我有用。

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