Structure can contain other Structure variables as members. This is called Nesting of Structures.
For example address structure is a structure. We can define a nested structure called customer which contains address structure as follows:
struct address { int house_number; char street_name[50]; int zip_code; char country[50]; }; struct customer { char name[50]; structure address billing_addr; structure address shipping_addr; }; struct customer cust1;
If the structure contains another structure, we can use dot operator to access nested structure and use dot operator again to access variables of nested structure.
cust1.billing_addr.house_number = 10;
It should be noted that a structure definition can’t contain the variable of its own type as a data member. This means the following structure definition is illegal.
struct address { int house_number; char street_name[50]; int zip_code; char country[50]; struct address old_address; /* The statement above is illegal*/ };
Consider another example. To store the address of an employee in a structure, store the attributes of address such as street number, city, state, and pin code of the employee into a separate structure and nest the structure address into the structure employee .Consider the following program.
# include <stdio.h> struct address { char city[20]; int pin; char phone[14]; }; struct employee { char name[20]; struct address add; }; void main() { struct employee emp; printf("Enter employee information?\n"); scanf("%s %s %d %s", emp.name, emp.add.city, & emp.add.pin, emp.add.phone); printf("Printing the employee information....\n"); printf("name: %s\nCity: %s\nPincode: %d\nPhone: %s", emp.name, emp.add.city, emp.add.pin, emp.add.phone); }
Enter employee information? Arun Delhi 110001 1234567890 Printing the employee information.... name: Arun City: Delhi Pincode: 110001 Phone: 1234567890
The structure can be nested in the following ways.
Here, we create two structures, but the dependent structure should be used inside the main structure as a member.
struct Date { int dd; int mm; int yyyy; }; struct Employee { int id; char name[20]; struct Date doj; }emp1;
As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a member in Employee structure. In this way, we can use Date structure in many structures.
The embedded structure enables us to declare the structure inside the structure. Hence, it requires less line of codes but it can not be used in multiple data structures. Consider the following example.
struct Employee { int id; char name[20]; struct Date { int dd; int mm; int yyyy; }doj; }emp1;
We can access the member of the nested structure by Outer_Structure.Nested_Structure.member as given below:
Let's see a simple example of the nested structure in C language.
#include <stdio.h> #include <string.h> struct Employee { int id; char name[20]; struct Date { int dd; int mm; int yyyy; }doj; }e1; int main( ) { //storing employee information e1.id=101; strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array e1.doj.dd=10; e1.doj.mm=11; e1.doj.yyyy=2014; //printing first employee information printf( "employee id : %d\n", e1.id); printf( "employee name : %s\n", e1.name); printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy); return 0; }
employee id : 101 employee name : John Abraham employee date of joining (dd/mm/yyyy) : 10/11/2014
Similar to other variables, a structure can also be passed to a function. In the below example pass the structure variable employee to a function display() which is used to display the details of an employee.
#include<stdio.h> struct address { char city[20]; int pin; char phone[14]; }; struct employee { char name[20]; struct address add; }; void display(struct employee); void main () { struct employee emp; printf("Enter employee information?\n"); scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone); display(emp); } void display(struct employee emp) { printf("Printing the details....\n"); printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone); }