A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name for convenient handling. Structures help to organize data. particularly in large programs. because they allow a group of related variables to be treated as a single unit.
Consider an example. In an organization, an employee's details (i.e., his name, address, designation, salary etc.) have to be maintained. One potential method would be to create a two-dimensional array, which would contain all the details. But in this case, this is not possible because the parameters are of different types, i.e., name is of type char, salary is of type float, and so on. There is a simple solution to this problem – structures.
Structure Declaration
Let us create a structure to illustrate the above mentioned example
struct employee
{
char *name;
char *address;
char *desig;
float salary;
} emp1, emp2
The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. The variables declared within the structure declaration are called its members. The struct declaration defines a datatype, and the variables emp1 and emp2, which follow the struct declaration, are defined as variables of this type and storage space is set aside for them. This is equivalent to declaring variables of type int or char.
A structure declaration that is not followed by a list of variables reserves no storage space; it merely describes the template. In other words, the structure is only defined. not declared.
The word following the keyword struct (employee in this case) is called a structure tag. and is optional. It is optional because it only names this new type that has been defined by the structure declaration, and can be used as a substitute later. For example, at a later stage. if a new variable emp3 of type struct employee has to be declared, it could be declared as
struct employee emp3;
There is no need to define the entire structure all over again.
A member of a structure is always referred to and accessed using the structure name.
Structure name.member name
The structure member access operator ".". connects the structure name and the member name. For example, to print the name of the employee.
printf ("The employee's name is %8",emp1.name);
Nested Structures
Nested Structures are nothing but structures within structures. Let us take the same example discussed above - the address in the employee structure can be a structure by itself, which stores the house number, the street name, the area name and the pin code.
struct emp add
{
int no;
char *street;
char *area;
int pincode;
};
The above definition could be used as a template to declare a variable address in the employee structure
struct employee
{
char name;
struct emp_add address;
char *desig;
float salary;
}emp1, emp2 ;
or the two definitions could even be consolidated as
struct employee
{
char *name;
struct emp_add
{
int no;
char *street;
char *area;
int pincode;
}
address;
char *desig;
float salary;
}
emp1, emp2;