Structure

We can not use an array if we represent a collection of data items of different types using a single name.Fortunately, C supports a constructed data type known as structures, a mechanism for packing data of different types.

Unlike arrays,Structures must be defined first for their formats that may be used later to declare structure variable.Let us use an example to illustrate the process of structure definition and the creration of structure variable.Consider a college database consisting of Student_name,Student_rollno,student_address.We can define a structure to hold the informations as follow:-

The keyword struct declares a structure to hold the details of three data fields.These fields are known as structure element or members.

Syntax of structure :-

After defining a structure format we can declare variable of that type.A structure variable declaration is same as declaration of variable of any other data type.A general form is:-

For example:-

Accessing members of a structure

We can access the member of a structure in a number of ways.

Member operator (.)

The link between a structure element or member and variable is established using dot operator(.) A general form is:-

variable_name . member_name
For example:-

Structure pointer operator()

If we want to access a structure pointer member then we use Structure pointer operator.

variable_name pointer_member_name
For example:-

student1 ptr_name

C Code Example