Structures
Introduction
Structure is a mechanism for packing data of different types.
A structure is
a collection of elements of different types.
A structure is
a convenient tool for handling a group of logically related data items.
Example: It
can be used to represent a set of attributes, such as student-name, roll- number and marks.
The concept of
structure is similar to records in many other languages.
Structures help
to organize complex data in a more meaningful way.
Defining a Structure:-
The general format of a structure definition is as follows.
struct tag-name
{
data-type
member1;
data-type
member1;
---------------------------
---------------------------
};
Example:
struct employee
{
char ename[20]
int eno;
float salary;
};
The keyword “struct” declares a structure to hold the details of 3
data fields, namely ename, eno and salary.
These data fields are called structure elements or members.
Each member may belong to different types of data.
Employee is the name of the structure and is called structure tag.
When a structure is declared, no memory will be allocated, only a
template will be created. This describes the format shown below:
ename array of
characters
eno integer
salary float
in defining a structure, you may note some points.
- The template is terminated with a semicolon (;).
- While the entire definition is considered as a statement, each member is declared independently.
- The tag name, such as employee is used to declared structure variable.