What is structure, How to define and declare the structure variable
Structure
A structure is a collection of data items of different types.
A structure is a convenient tool for handling a group of logically related data items.
For example it can be used to represent a set of attributes, such as student_name, roll_number and marks.
Difining a Structure
Structures must be defined first for their format that may be used later to declare structure variable.
The general format of a structure definition is as follows :
For example:
struct tag_name
{
data_type member1;
data_type member2;
……………………
……………………
};
In defining a structure you may note the following syntax :
The template is terminated with semicolon.
While the entire definition is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template.
The tag_name can be used to declare structure variables of its type, later in program.
Declaration of Structure
After defining a structure format we can declare variables of that type.
A structure variable declaration is similar to the declaration of variables of any other data types.
It includes the following elements :
The keyword struct.
The structure tag name.
List of variable names separated by commas.
A terminating semicolon.
For example : struct student s1,s2;
The members of a structure themselves are not variables. They do not occupy any memory until they are associated with the structure variables such as student.
When the complier comes across a declaration statement, it reserves memory space for the structure variables.
It is also allowed to combine both the structure definition and variables in one statement.