structures within structures

·

structure with in a structure means nesting of structures. Let us consider the following
struc- ture defined to store information about the salary of employees.

struct salary
{
char name[20];
char department[10];
int basic_pay;
int dearness_allowance;
int city_allowance;
}
employee;

This structure defines name, department, basic pay and 3 kinds of allowance. we can group all the items related to allowance together and declare them under a substructure as shown below:

struct salary
{
char name [20];
char department[10];
struct
{
int dearness;
int hous_rent;
int city;
}
allowance;
}
employee;
The salary structure contains a member named allowance which itself is a structure with 3 members. The members contained in the inner, structure namely dearness, hous_rent, and city can be referred to as :

employee .allowance. dearness
employee. allowance. hous_rent
employee. allowance. city

An inner-most member in a nested structure can be accessed by chaining all the concerned. structure variables (from outer-most to inner-most) with the member using dot operator. The following being invalid.

employee. allowance (actual member is missing)
employee. hous_rent (inner structure variable is missing)

About Me

Blog Archive