Structures
Structures
Topic
1-Structures
1.1.1-Declaring a structure
1.1.2-Defining a structure variable
1.1.3-Accessing members of structure variable
1.1.4-Initializing structure variables at declaration
1.1.5-Assigning one structure variable to other
1.1.6-Array as member of structure
1.1.6.1-Initialzing a structure with array as member
1.2-Arrays of structures
1.2.1-Initializing Array of structures
1.3- Nested Structure
1-Structures
A structure is a collection of multiple data types that can be referenced with single name. It may
contain similar or different data types. The data items in a structure are called structure
members, elements or fields. The structures are used to join simple variables together to form
complex variables.
1.1-Declaring a structure
Syntax
Struct structure name
{
Data type1 identifier1;
Data type2 identifier2;
};
e.g
struct Student
{
Int RollNo;
Int Marks;
Float avg;
Char Grade;
};
Global Institute of Computer Science
1.2 Defining structure variable
Student ali;
Example:
student ali;
ali.RollNo=20;
ali.Marks=89;
ali.avg=67.6;
ali.Grade=’a’;
/*
write a program that declares a structure Student to store Roll No, Marks, Average and
Grade of a student. The program should define a structure variable, input the
values and then displays these values.
*/
#include<iostream.h>
struct Student
{
int rollNo;
int marks;
float avg;
char grade;
};//end of structure Student
int main()
{
Student s;
cout<<"Enter Roll No";
cin>>s.rollNo;
cout<<"Enter Marks";
Global Institute of Computer Science
cin>>s.marks;
cout<<"Enter Average";
cin>>s.avg;
cout<<"Enter Grade";
cin>>s.grade;
cout<<"You Entered following details of student:\n";
cout<<"Roll No:"<<s.rollNo<<endl;
cout<<"Marks:"<<s.marks<<endl;
cout<<"Average:"<<s.avg<<endl;
cout<<"Grade:"<<s.grade<<endl;
}//end of main
#include<iostream.h>
struct Birth
{
int day;
int month;
int year;
};//end of structure Birth
int main()
{
Birth b;
//cout<<"Memory Occupy by structure variable"<<sizeof(b)<<endl;
cout<<"Enter Date of Birth:";
cin>>b.day;
cout<<"Enter Month of Birth:";
cin>>b.month;
cout<<"Enter Year of Birth:";
cin>>b.year;
cout<<"Your Complete date of birth is:"<<b.day<<"/"<<b.month<<"/"<<b.year;
}//end of main
Global Institute of Computer Science
/* Example .3
write a program that declares a structure named Book to store BookID,price and
pages of a book. it defines two variables and inputs values. it displays the record
of most costly book..Note you have to compare the book by price
*/
#include<iostream.h>
struct Book
{
int BookID;
int pages;
float price;
void main()
{
Book first;
Book second;
cout<<"Enter first book Details"<<endl;
cout<<"Enter BookID:";
cin>>first.BookID;
cout<<"Enter Book Pages:";
cin>>first.pages;
cout<<"Enter Book Price:";
cin>>first.price;
}//end of main
Syntax
Struct_Name identifier={value1,value2…..};
Example:
Student ali={1,223,89,’A’};
/* Example .4
Initialize structure variable at the time of declaration
write a program that declares a structure Employee to store employee id and salary of an
employee. it defines and initializes a structure variable and displays it.
*/
#include<iostream.h>
struct Employee
{
int empId;
int esalary;
};//end of Employee structure
Global Institute of Computer Science
//Employee em; //if you declare structure variable outside the main then initialize to zero
//if you declare structure variable inside the main then initialize
to garbage
int main()
{
Employee e={1,5000};
cout<<"Employee ID:"<<e.empId<<endl;
cout<<"Employee Salary:"<<e.esalary<<endl;
} //end of main
/* Example .5
Initialize structure variable at the time of declaration
write a program that declares a structure Phone to store three parts of phone number i.e country
Code, City Code and number separately.
create two variables of structure phone, initialize one variable at the time of declaration and get
inputs from the user in the second variable and displays both of them.
*/
#include<iostream.h>
struct Phone
{
int countrycode;
int citycode;
long number;
};//end of Phone structure
/* Example .6
write a program that uses a structure named as PayRoll to store employee ID, name, hours
worked, hourly rate and gross pay.
the program inputs employee number, name, hours worked and hourly rate. the user calculates
gross pay and then displays all employee data on the screen.
note:
the gross pay=hours worked*hourly rate;
Global Institute of Computer Science
*/
#include<iostream.h>
#include<stdio.h>
struct PayRoll
{
int empID;
char firstname[50];
char lastname[50];
double hoursworked;
double hourlyrate;
double grosspay;
int main()
{
PayRoll employee;
cout<<"Enter Employee ID:";
cin>>employee.empID;
cout<<"Enter Employee First name:";
gets(employee.firstname);
cout<<"Enter Employee Last name:";
gets(employee.lastname);
cout<<"Enter Number of hours worked by Employee:";
cin>>employee.hoursworked;
cout<<"Enter hourly rate of Employee:";
cin>>employee.hourlyrate;
employee.grosspay=employee.hoursworked*employee.hourlyrate;
cout<<"-------EMPLOYEE PAY ROLL DATA---------"<<endl;
cout<<"Enter Employee ID:"<<employee.empID<<endl;
cout<<"Enter Employee First name:"<<employee.firstname<<endl;
cout<<"Enter Employee Last name:"<<employee.lastname<<endl;
cout<<"Enter Number of hours worked by Employee:"<<employee.hoursworked<<endl;
cout<<"Enter hourly rate of Employee:"<<employee.hourlyrate<<endl;
cout<<"Gross Pay of "<<employee.firstname<<"
"<<employee.lastname<<":"<<employee.grosspay<<endl;
Global Institute of Computer Science
} //end of main
Int main()
{
Phone p1 = {92,42,420420}; //if you don’t initialize any index than by default it initializes to
zero
Phone p2;
cout<<"Enter Country Code of Second Phone:";
cin>>p2.countrycode;
cout<<"Enter City Code of Second Phone:";
cin>>p2.citycode;
cout<<"Enter number of Second Phone:";
cin>>p2.number;
} //end of main
*/
#include<iostream.h>
struct Marks
{
int m;
char grade;
Global Institute of Computer Science
};//end of sturcture Marks
int main()
{
Marks first,second;
cout<<"Enter Marks in first";
cin>>first.m;
cout<<"Enter Grade in first";
cin>>first.grade;
cout<<"-------------------------------------"<<endl;
cout<<"Second Record is as follows:"<<endl;
cout<<"Marks"<<second.m<<endl;
cout<<"Marks"<<second.grade<<endl;
}//end of main