Professional Documents
Culture Documents
Lab-10
For Students:
Roll No
Name
Class
Instructor
_________________________
Mr. Irfan Ullah
_____________________________________________________________________________1
14
Programming Fundamentals - Lab [COSC-1201]
10.1. OBJECTIVES
1. Structures
2. Declaring a Structure
3. Defining Structure Variable
4. Accessing members of structure
5. Initializing a Structure Variable
6. Array of Structure
10.2. Structure
Structure is a collection of variables of different data types under a single name. We can say a
structure as a user defined datatype.
struct Person
{
char name[50];
int age;
float salary;
};
Here a structure person is defined which has three members: name, age and salary. When a
structure is created, no memory is allocated.
Person bill;
When structure variable is defined, only then the required memory is allocated by the compiler.
If we want to access age of structure variable bill and assign 50 to it. You can perform this task
by using following code:
bill.age = 50;
_____________________________________________________________________________1
15
Programming Fundamentals - Lab [COSC-1201]
10.6. Initializing a Structure Variable
A structure variable can be initialized as:
Syntax:
struct book
{
int bookid;
int pages;
float price;
_____________________________________________________________________________1
16
Programming Fundamentals - Lab [COSC-1201]
};
book b[3];
The above line of code declares a structure Book. Then defines an array of structures b[3]. This
structure now can store the record of three Books.
_____________________________________________________________________________1
17
Programming Fundamentals - Lab [COSC-1201]
10.9.2. Program no 2
Write a program that declares a structure to store roll no and marks of five subjects. It defines a
structure variable, inputs the values and displays roll no, total marks and average marks.
_____________________________________________________________________________1
18
Programming Fundamentals - Lab [COSC-1201]
10.9.3. Program no 3
Write a program that declares a structure to store id, pages and price of a book. It defines an
array of structures to store the records of five books. It inputs the record of five books and
_____________________________________________________________________________1
19
Programming Fundamentals - Lab [COSC-1201]
display the record of most costly and least costly book.
_____________________________________________________________________________1
20
Programming Fundamentals - Lab [COSC-1201]
_____________________________________________________________________________1
21
Programming Fundamentals - Lab [COSC-1201]