-->

Structures within Structures

Structures within Structures
Structures within Structures
·         Structures within a structure means nesting of structures
·         Nesting of structures is permitted in C language
Example:
struct salary
{
char name;
char department;
int basicpay;
int hra;
int da;
int ea;
}employee;
·         This structure defines a name, department, basic pay and three kinds of allowances.
·         We can group all the items related to allowances together and declare them under a substructure as shown below.
struct salary
{
char name;
char department;
struct
{
int da;
int hra;
int ca;
}allowance;
}employee;
·         The salary structure contains a member named allowance which itself is a structure with three members, as shown here
employee. allowance.da
employee. allowance.hra
employee. allowance.ca
·         An innermost member in a nested structure can be accessed by chaining all the concerned structure variables (from the outermost to the innermost)with the member using a dot operator.
The following are invalid.
employee.allowance (in this actual member is missing).
employee.hra (inner structure variable is missing)
An innermost structure can have more than one variable
·         A The following form of declaration is legal
struct salary
{
---------------
---------------
struct
{
int da;
---------------
---------------
}allowance, arrears;
}employee[3];
·         The inner-most structure has two variables, allowance and arrears.
·         This implies that both of them have the same structure template.
·         Note the comma after the name allowance.
·         A base member can be accessed as follows.
employee[1].allowance.da
employee[1].arrears.da
·         We can also use a tag name to define inner structures.
Example:

struct pay
{
int da;
int hra;
int city;
};

struct salary
{
char name;
char department;
struct pay allowance;
struct pay arrears;
};
struct salary employee[3];
·         It is also possible to nest more than one type of structure.
Example:
struct personal record
{
struct name-part name;
struct addrpart address;
-------------------------------
-------------------------------
}
struct personal record p;
·         The first member of this structure is the name, which is of the type struct name-part.
·         Similarly, other members have their structure types.

Related Posts

Subscribe Our Newsletter