0% found this document useful (0 votes)
38 views7 pages

Lab 6 DSA

This document discusses structures in C++. Structures allow grouping of different data types together under one name. The document explains how to define a structure, structure variables, access members of a structure, pass structures to functions, and return structures from functions. It provides examples of each concept and ends with tasks to write programs using structures.

Uploaded by

muhammad arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views7 pages

Lab 6 DSA

This document discusses structures in C++. Structures allow grouping of different data types together under one name. The document explains how to define a structure, structure variables, access members of a structure, pass structures to functions, and return structures from functions. It provides examples of each concept and ends with tasks to write programs using structures.

Uploaded by

muhammad arslan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab#6 Structures

Objectives:

The objectives of this lab are to Understand the structure, its members and how to use structures
in a program.

Introduction:

Structures form a very large building block with which to collect like data into one collective
unit.Arrays are one of the most widely used data structures in programming languages. One
downfall of using such a data type is that one must use homogeneous data types, an array can
only hold multiple items of the same type. Structures overcome this problem by allowing the
programmer to have an unlimited number of items of different data types.

How to define a structure in C++ programming?

The struct keyword defines a structure type followed by an identifier(name of the structure).
Then inside the curly braces, you can declare one or more members (declare variables inside
curly braces) of that structure. For example:

Here a structure person is defined which has three members: name, age and salary.

When a structure is created, no memory is allocated. The structure definition is only the blueprint
for the creating of variables. You can imagine it as a datatype.

How to define a structure variable?

Once you declare a structure person as above. You can define a structure variable as:

Here, a structure variable bill is defined which is of type structure person. When structure
variable is defined, then only the required memory is allocated by the compiler. Considering you
have either 32-bit or 64-bit system, the memory of float is 4 bytes, memory of int is 4 bytes and
memory ofchar is 1 byte. Hence, 58 bytes of memory is allocated for structure variable bill.

How to access members of a structure?

The members of structure variable is accessed using dot operator. Suppose, you want to
access ageof structure variable bill and assign it 50 to it. You can perform this task by using
following code below:

Example 1: C++ Program to assign data to members of a structure variable and display it.
Here a structure person is declared which has three members. Inside main() function, a structure
variable p1 is defined. Then, the user is asked to enter information and data entered by user is
displayed.

Passing structure to function in C++:

A structure variable can be passed to a function in similar way as normal argument. Consider this
example:

#include<iostream>

usingnamespacestd;

struct person {

char name[50];

int age;

float salary;

};

voiddisplayData(person);// Function declaration

int main(){

person p;

cout<<"Enter Full name: ";

cin.get(p.name,50);

cout<<"Enter age: ";

cin>>p.age;

cout<<"Enter salary: ";


cin>>p.salary;

displayData(p);// Function call with structure variable as arugment

return0;

voiddisplayData(person p1){

cout<<"\nDisplaying Information."<<endl;

cout<<"Name: "<< p1.name <<endl;

cout<<"Age: "<< p1.age <<endl;

cout<<"Salary: "<< p1.salary;

In this program, user is asked to enter the data for the members of a structure variable
inside main()function. Then that structure variable to passed to a function using code.

The return type of displayData() is void and one argument of type structure person is passed. The
function prototype also should match the function definition. Then the members of
structure p1 is displayed from this function.
Returning structure from function in C++:

#include<iostream>

usingnamespacestd;

struct person {

char name[50];

int age;

float salary;

};

person getData(person);

voiddisplayData(person);

int main(){

person p;

p =getData(p);

displayData(p);

return0;

person getData(person p1){


cout<<"Enter Full name: ";

cin.get(p1.name,50);

cout<<"Enter age: ";

cin>> p1.age;

cout<<"Enter salary: ";

cin>> p1.salary;

return p1;

voiddisplayData(person p1){

cout<<"\nDisplaying Information."<<endl;

cout<<"Name: "<< p1.name <<endl;

cout<<"Age: "<< p1.age <<endl;

cout<<"Salary: "<< p1.salary;

In this program, the structure variable p of type structure person is defined


under main() function. The structure variable p is passed to getData() function which takes input
from user and that value is stored in member function of structure variable p1 which is then
returned to main function.
Note: The value of all members of a structure variable can be assigned to another structure using
assignment operator = if both structure variables are of same type. You don't need to manually
assign each members.

Then the structure variable p is passed to displayData() function, which displays the information.

Lab Tasks

Program #1: Write a program that declare a structure to store Name, Roll_no, marks and section
of a student. The program should define a structure variable, input values of two students and
then display their values.

Program #2: Write a program that declares a structure to store book author, pages and its cost. It
defines two structure variables. The program displays the book author and the pages of costly
book.

Program # 3: Write a program that declares a structure to store book author, pages and its cost.
It defines a structure array of size five. The program displays the book author and the pages of
costly book.

Program # 4: Write a program that declares a structure to store Name, Roll_no, marks and
section of a student. The program should define a structure variable and take input values from
user. It passes the variable to function that displays its contents.

You might also like