Pr.Pg Next Pg

Structures tutorials

  • If all the data elements of a program are of the same data types, it can be stored in Array. For example Marks of 10 students as mark[10], Roll no of 10 students as Rollno[10] and so on.

  • However if all the data elements of a programs are from different data types then in such cases Array can't be used.

  • For example if you need to store data of several students, then there are different data types and for such case Structure is suitable.

  • A structure is a collection of data of similar or different data types. It can represent a record.

  • Structure is a collection of logically related data items, grouped together under a single name called structure tag.

  • The data items that make up a structure are called members or fields.

  • Since a structure is derived from other data types, it is called a derived data type or user defined data types.

  • In short a structure is a heterogeneous user defined data type that contains different data types.

 

Declaring a structure

  • Declaration of structure is called structure specifiers.

  • Structure specifiers is a declaration, hence memory is not allotted.

  • A function may have declaration as well as definition, whereas structure must have declaration as well as definition.

  • Unlike functions, declaration is compulsory for structure.

  • The declaration starts with the reserved word struct, followed by the name of structure.

  • Following structure name it begins with curly braces.

  • Within { } curly braces, declaration specify the members of the structure.

  • Below is the simple example of structure student

struct student

{

int rollno ;

char name[20] ;

float percent ;

};

  • In above example name of the structure is student, which acts as data type for defining variable.

  • The identifier student names the structure, and is called the structure tag or simply tag.

  • Within { } we have declared rollno, name and percent are members or field.

  • Members can be of any data types. It can be common data types such as (int,float,char),pointer, array, structure or even bit fields.

  • A structure can also be member of another structure, which is called as nested structure.

  • The closing bracket "}" is followed with semicolon ";".

  • The structure members are maintained in contiguous memory locations.

 

Declaring and initializing structure variables

  • Structure variables can be declared in following ways.

  • In structure declaration

  • Using structure tag

 

Declaring structure variable using structure declaration

  • The structure variables can be declared after closing curly brace.

  • For example

 

struct student

{

int rollno ;

char name[20] ;

float percent ;

} s1,s2,s3;

 

Declaring structure variable using structure tag

  • The variables of structure may also be declared separately by using the structure tag as follows.

struct student s1,s2,s3;

or

student s1,s2,s3;

Pr.Pg border                                              Next Pg