Pages

Friday, February 12, 2010

Nested structure

What is a nested structure ?


A nested structure is a structure defined inside another structure. We have already learnt about nested if’s and nested loops. In case you have skipped it let’s do a quick revision. A nested if is “an if inside another if” and a nested loop is “a loop inside another loop”. for example a nested if will look like this

if(condition 1)


{


                     if(condition 2)


                    {


                   }


                 else


               {


              }


}


else

And this is a nested for loop

for(i=0;i<10;i++)


{


for(j=0;j<4;j++)


{


}


}

As you can see in both the codes above one if (or for) is purely inside the other that’s why it is called nested. We can also create nested structures in the same way where one structure will be written completely inside the other.

Why are they used ?

The simplest answer of this question is REUSABILITY. A nested structure helps us to reuse the properties of an existing structure from other structrure. The main benefit that we will get is that we won’t have to re declare all those variables again which cut the memory needs of the program not to mention saving of time and effort. Let’s see how it works

A school wants to maintain data of its teachers and students. Following is the information that we want to maintain.

Teachers : name,address,teleno,qualification, salary and date of joining.


Students : name,father’s name, address,teleno, and class.

As you can see some properties of both the persons (name,address and teleno) are same while some of them are different. if we create structures of the above entities we will have to write the code twice, once each for students and teachers. This will demand not only more resources but also time and effort of the programmer. Let’s see the incorrect version first and then we will jump to the correct one.

Example 1 : creating 2 different structures from scratch.

struct students


{


char name[20];


char fname[20];


char address[30];


long teleno;


int clas;


};


struct teachers


{


char name[20];


char address[30];


long teleno;


int salary;


char doj[12];


};



Going by the above structures is quite clear that we have 3 variables declared in both the structures. Imagine what would happen if there are many more variables that are common like (height, weight, blood group etc.). it can also happen that we want to store data of non teaching staff separately what will happen then ? we will have to create dozens of variables for each teachers,students, and non teachers. This is definitely going to create chaos since managing so many variables is not easy. Declaring same variables again and again will also create inconsistency in all the structures. for example somewhere we have created the address variable with a size of 30 and at some other place we can give the size as 20. This will cause problems later on if we try to copy the strings and do some processing that involve different size strings. Let’s recreate the structures now using the nest concept to see if it helps.

Step 1 : Create a structure that will contain the common variables.

struct person


{


char name[20];


char address[30];


long teleno;


};

Step 2 : Create another structures that will use the above structure.

struct students


{


person stud_data; //using person as a datatype


char fname[20];


int clas;


}

Step 3 : Create an object of the second structure and intiailsize the properties.

students mystudents; // creating an object


printf(“\nEnter name : “);


scanf(“%s”,&mystudents.stud_data.name);


printf(“\nEnter address : “);


scanf(“%s”,&mystudents.stud_data.address);


printf(“\nEnter teleno : “);


scanf(“%ld”,&mystudents.stud_data.teleno);


printf(“\nEnter father’s name : “);


scanf(“%s”,&mystudents.stud_data.fname);


printf(“\nEnter class : “);


scanf(“%d”,&mystudents.stud_data.clas);



The complete code

struct person


{


char name[20];


char address[30];


long teleno;


};


struct students


{


person stud_data; //using person as a datatype


char fname[20];


int clas;


}


struct teachers


{


person tdetails;


char quail[10];


int salary;


char doj[12];


};


void main()


{


students mystudents; // creating an object of students


teachers myteachers; // creating an object of teachers






printf(“\nENTER STUDENT’s DATA”);


printf(“\nEnter name : “);


scanf(“%s”,&mystudents.stud_data.name);


printf(“\nEnter address : “);


scanf(“%s”,&mystudents.stud_data.address);


printf(“\nEnter teleno : “);


scanf(“%ld”,&mystudents.stud_data.teleno);


printf(“\nEnter father’s name : “);


scanf(“%s”,&mystudents.fname);


printf(“\nEnter class : “);


scanf(“%d”,&mystudents.clas);






printf(“\nENTER TEACHER’s DATA”);


printf(“\nEnter name : “);


scanf(“%s”,&myteachers.tdetails.name);


printf(“\nEnter address : “);


scanf(“%s”,&myteachers.tdetails.address);


printf(“\nEnter teleno : “);


scanf(“%ld”,&myteachers.tdetails.teleno);


printf(“\nEnter qualification : “);


scanf(“%s”,&myteachers.quali);


printf(“\nEnter date of joining : “);


scanf(“%s”,&myteachers.doj);






printf(“\n DATA ENTRY COMPLETE. NOW PRINTING DATA…”);

printf(“\nPRINTING STUDENTS DATA”);


printf(“\n-+-+--+-+--+-+--+-+--+-+--+-+--+-+-“);// just a separator, can be skipped


printf(“\nName : %s”,mystudents.stud_data.name);


printf(“\nAddress : %s”,mystudents.stud_data.address);


printf(“\nTeleno : %ld”,mystudents.stud_data.teleno);


printf(“\nFather’s name : %s”,mystudents.fname);


printf(“\nClass : %d”,mystudents.clas);






printf(“\nPRINTING TEACHERS DATA”);


printf(“\n-+-+--+-+--+-+--+-+--+-+--+-+--+-+-“);


printf(“\nName : %s”,myteachers.tdetails.name);


printf(“\nAddress : %s”, myteachers.tdetails.address);


printf(“\nTeleno : %ld”, myteachers.tdetails.teleno);


printf(“\nQualification : %s”, myteachers.quali);


printf(“\nDate of joining : %s”, myteachers.doj);


}

Description

In the above code firstly we have created a structure named “person” which houses the common properties of both students and teachers. The second structure named “students” then uses the “person“ structure as a datatype (don’t forget structures are user defined datatypes). The statement “person stud_data” denotes that the variable “stud_data” has its datatype as “person”. This allows the “students” structure to access the properties of the person structure using a special syntax which is “objectname.variablename.parentvariablename”. the student structure has just three member variables of its “own” – stud_data, fname and clas but since it has a reference of the person structure inside, it can also access person’s variables also.

0 comments:

Post a Comment