Structure

Structure is a collection of heterogeneous elements. Heterogeneous means the elements may b differ in their data-type. Structure is very useful to make grouping of different type of data members.

Syntax:           

struct structure-name

{

Data-member1;

Data-memeber2;

———————
———————

Data-member n;

};

The above syntax is shown how the structure is created. And in the first line we have the keyword ‘struct’, and then the name of the structure. Then we can give the data-members according to our requirement. And these data members can be of any type. Then to terminate a structure a semicolon is must at the end.

e.g:    

struct student

{

int roll_num;

char name[20];

float marks;

}

Structure variables: Structure variables are used to access the members of the structure. To declare a structure variable we need the struct keyword, structure name, variable names then the semicolon.

 

There are two methods to declare a structure:

1.      Syntax:      struct structure-name variable-1,variable-2,……,variable-n;

e.g:            struct student s1, s2, s3;

In this type of declaration we declared this in the main() function.

2.      Syntax:     

struct structure-name

{

Data member;

——————-

——————-

} variable-1, variable-2, variable-n;

In this type of declaration the variables are declared at the end but before semicolon.

More:

Structure initialization
Structure within structure
Array of structure
Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *