Nested Structure in C
C provides us the feature of nesting one structure within
another structure by using which, complex data types are created.
Fig: Nested structure in c
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);
}
Output
Enter employee information?
Pramod
Delhi
201301
9540225110
Printing the employee information....
name: Pramod
City: Delhi
Pincode: 201301
Phone: 9450225110
The
structure can be nested in the following ways.
1.
By
separate structure
2.
By
Embedded structure
1) Separate structure
We
create two structures, but the dependent structure should be used inside the
main structure as a member.
Example
struct Date
{
int dd;
int mm;
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
We
create two structures, but the dependent structure should be used inside the
main structure as a member.
Example
struct Date
{
int dd;
int mm;
int yyyy;
};
struct Employee
{
int id;
char name[20];
struct Date doj;
}emp1;
2) Embedded structure
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.
Example:
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
} emp1;
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.
Example:
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
} emp1;
Accessing Nested Structure
We
can access the member of the nested structure by Outer_Structure.
e1.doj.mm
e1.doj.yyyy
We
can access the member of the nested structure by Outer_Structure.
e1.doj.mm
e1.doj.yyyy
Program
#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( )
{
e1.id=101;
strcpy(e1.name, "Pramod Dwivedi");
e1.doj.dd=08;
e1.doj.mm=07;
e1.doj.yyyy=2000;
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.d d ,e1.doj.mm,e1.doj.yyyy);
return 0;
}
Output:
employee id : 101
employee name : Pramod Dwivedi
employee date of joining (dd/mm/yyyy) : 08/07/2000
#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( )
{
e1.id=101;
strcpy(e1.name, "Pramod Dwivedi");
e1.doj.dd=08;
e1.doj.mm=07;
e1.doj.yyyy=2000;
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.d d ,e1.doj.mm,e1.doj.yyyy);
return 0;
}
Output:
employee id : 101
employee name : Pramod Dwivedi
employee date of joining (dd/mm/yyyy) : 08/07/2000
No comments:
Post a Comment