PF LAB 13
PF LAB 13
Course Instructor:
Lab Instructor:
Defining a Structure:
A structure is declared by using the keyword struct followed by the structure name. The structure members
are defined with their type inside the opening and closing braces { }. The closing braces is ended with a
semicolon.
The syntax for declaring a structure is as follows:
struct Struct_name
{
Data_type1 identifier1;
Data_type2 identifier2;
};
2
Identifier1: it indicates the name of first member of the structure.
Data_type2: it indicates data types of second member of the structure.
Identifier2: it indicates the name of the second member of the structure.
Defining a Structure Variable :
The structure variable can be defined after the declaration of a structure. The process of defining a structure
variable is same as defining a variable of basic types such as int and char. The definition tells the
compiler to allocate memory space for the variable.
Syntax:
The syntax of defining a structure variable is as follow:
struct_Name Identifier;
3
The first line declares a structure variable abc and initializes it. The second line declares another structure
variable xyz and initializes it with the values of abc.
Array as Member of Structure:
A structure may consist of different types of data . The members variable can be simple data type such as
int, char and float. The member variables can also be complex like arrays.
Example:
struct student
{
int rollno;
int marks[10];
};
The above example declares a structure student with two members. the first member is a single variable of
type int. The second member is an array of integers. It can be used to store the marks of five students.
Array of Structures:
An array is a collection of same type of data . An array can be of simple data type such as int, char or
float etc. Similarly, a array can be of users define type such as a structure .An array of a structure is type
of array in which each element contains a complete structure. It can be used to store many records.
Example:
struct book
{
int
book_id;
int pages;
float
price; };
book b[5];
The above example declares a structure book. It defines an array of structures b[5]. The array can store
the records of five books.
Nested Structure:
A structure within a structure is known as nested structure. A nested structure is created when the member
of a structure is itself a structure.
Example:
4
struct A{
int n;
float b;
}; struct
B{ char c;
A x;
};
The above example declares two structures A and B. The structure A contains simple member variables n
and b. The structure B also contains two member variables. The first is character variable c. and the second
is structure variable x.
14.4 Examples
Example 14.1: Write a program to store and display information using structure
#include <iostream>
using namespace std;
struct student
{ char name[50];
int roll;
float marks;
};
int main()
{ student s;
cout << "Enter information: " << endl;
cout << "Enter name: "; cin >>
s.name;
cout << "Enter roll number:
"; cin >> s.roll; cout <<
"Enter marks: "; cin >>
s.marks;
cout << "\nDisplaying Information:" << endl; cout
<< "Name: " << s.name << endl; cout <<
"Roll: " << s.roll << endl; cout << "Marks:
" << s.marks << endl; return 0;
}
5
The output of the program is:
Example 14.2: Write a program that declares a structure to store roll number and marks of
five subjects. It defines a structure variable, inputs the values and displays roll no, marks and
average marks.
#include <iostream>
using namespace std;
struct test
{ int
rno;
int
m[5];
};
int
main()
{ test r; int i, t=0;
float avg= 0.0;
cout<<"Enter Roll
No: "; cin>>r.rno;
for(i=0; i<5; i++)
{ cout<<"Enter marks for subject "<<i+1<<": ";
cin>>r.m[i]; t = t+r.m[i];
}
avg = t/5.0;
cout<<"Roll No: "<<r.rno<<endl;
cout<<"Total marks: "<<t<<endl;
cout<<"Average: "<<avg<<endl;
return
0;
}
6
14.5 Exercises for lab
Exercises 14.1: Write a program that declares a structure to store the distance covered by a player
along with the minutes and seconds taken to cover the distance. The program should input the records
of two players and then display the record of the winner.
Exercises 14.2: Write a program that declares a structure to store the code number, salary and grade
of an employee. The program defines two structure variables, input records of two employee and then
display the record of the employee with more salary.
1. Write a program that uses two structures Result and Record. The Result structure store marks
and grade, Record structure stores roll number and a Result type. The program declares a
variable of type Record, input roll number, marks and grade. It finally displays these values on
the screen.
2. Write a program that demonstrates the passing of entire structure as parameters to the called
function.
3. Write a program that demonstrates the passing address of the structure variable to the called
function using pointers.