STRUCTUIRE
A structure ia a collection of heterogeneous variable referenced under one name. The keyword struct tell the compiler that a structure is being defined.
General syntax:
Struct tag
{
Type variable name1;
Type variable name2;
:
:
:
}structure variable;
There are two ways to declared a structure variable i.e.
1.
Struct student
{
Char name[20];
Int roll_no;
Float marks;
} s1;
2.
Struct student
{
Char name[20];
Int roll_no;
Float marks;
} ;
Student s1;
Different points regarding structure:
• The moment structure variable is declared the memory is assign to the structure.
• Tag name or structure variable may be omitted but not both.
• The struct tag can be omitted when only one structure variable is needed.
• More than one structure variable can be created by using commas.
Example: Write a structure to hold students related information like roll no, class, marks, and grade. Definitions should also declared two variable senior and junior.
Solution:
Struct student
{
Char name[20];
Int roll_no;
Float marks;
Int class;
Char c;
} senior _student, junior _student;
Once the structure variable has been defined the structure can be exist to the structure variable and dot(.) variable.
General syntax:
Struct variable.struct element
Example: coutsenior_student.name;
INITILIZATION OF STRUCTURE ELEMENT
Structure element can be initialized either separately using assignment operator or jointly using the notation similar to array.
Example:
ss.name = “Pankaj”;
ss.roll_no = 1;
ss.marks = 65.5;
ss.class = 12;
ss.c = ‘A’;
OR
student.ss = {“Pankaj”,1, 65.5, 12, ‘A’};
Example 2: using structure display the cost of a car according to it make model and chases number by assigning all the pieces of necessary information.
Solution:
#include
#include
Struct car
{
Char car_make[20];
Int car_model;
Int number;
Float cost;
} c;
Void main()
{
Clrscr();
c.car_make = “Maruti”;
c.car_model = 2000;
c.number = 39456;
c.cost = 100050.00;
coutee
{
Char first_name[20];
Char second_name[20];
Int emp _id;
} emp[50];
Void main()
{
Int n,i;
Coutn;
For(i=0;iemp[i].first_name