A structure is a programmer defined datatype where group of different types of data are stored. Like nested loops C permits us to create structure with in another structure. Such structures are called nested structures. Such a nesting of structure more useful when you are working with complex data. In C, structures can be nested in two ways.
Defining One structure as a member of another structure
One structure can be defined as member of another structure. Let us consider below example, where an employee structure is defines as a member of department structure.
Example:
struct department
{
char name[30];
struct employee
{
int eid;
int sal;
char eaddress[20];
}e;
}d;
The innermost members in nested structure (employee) can be accessed as follows.
printf("\n the employee id %d", d.e.eid);
Prints("\n the employee salary %d", d.e.sal);
Defining Nested structure using structure variables
One can also define nested structure using structure variables. Here we define one structure variable as a member of another structure. Let us consider the following example,
struct dept
{
int name[30];
int no;
}
struct employee
{
int eid;
int easl;
struct dept deptname; // deptname is structure variable of type dept
}
Here in this example, I am defining "deptname" as a member of structure employee.
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.