Mahi Mahi - 2 years ago 100
C Question

Accessing a typedef structure inside another typedef struct

Kindly, see the below given snippet. How can we access the

descrptn
from the
struct sample
, using the
struct storage
?

typedef struct {
char hex;
char descrptn[50];
}TypeText;

typedef struct {
int val;
TypeText test[10];
}TypePara;

static const TypeText sample[]={
{0x00, "Low"},
{0x7F, "Mid"},
{0xFF, "HIgh"},
};

const TypePara storage[]= {
{0, sample},
{1, sample}
};

Answer Source

Actually I found my solution, in this way:

        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: %d",Class_Details[0].Students->Rank);
            printf("\r\nTest: %d",(Class_Details[0].Students+1)->Rank);

            return 0;
        }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download