Explain Structures with the help of an example.

A structure is a group of data of different data types. A structure must be defined before it is declared. A structure template is created during structure definition. Once the definition is given, structure may be declared. Memory is allotted to a structure only after declaration.


Syntax for structure definition :
struct structure_name
{
datatype member_1;
datatype member_2;
………
datatype member_n;
};

Example for structure definition:
Struct student
{
int roll_no;
int age;
char name[20];
};

Syntax for structure declaration:
struct <structure_name>    v1, v2, v3,…….., vn;
Example : 
struct student    s1, s2, s3;


To access the members of a structure .(dot) operator is used.
Example:
scanf(“%d%d%s”, &s1.roll_no, s1.age, s1.name);
scanf(“%d%d%s”, &s2.roll_no, s2.age, s2.name);
scanf(“%d%d%s”, &s3.roll_no, s3.age, s3.name);


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: