You are on page 1of 6

National University of Technology

Electrical Engineering Department

EE-1004: Introduction to Computer Programing Semester: IV

Lab 08: STRUCTURES

Learning objectives

 Brief understanding of Structures and its syntax.


 Understanding how to define a structure.
 Understanding how to pass structure to a function and return of structure from a
function
 Familiarization with the array of structures

Group:

Date of Experiment:

Instructor’s Signature:
A STRUCT is a C++ data structure that can be used to store together elements of different data
types. In C++, a structure is a user-defined data type. The structure creates a data type for grouping
items of different data types under a single data type.

For example:

Suppose you need to store information about someone, their name, citizenship, and age. You can
create variables like name, citizenship, and age to store the data separately.

However, you may need to store information about many persons in the future. It means variables for
different individuals will be created. For example, name1, citizenship1, age1 etc. To avoid this, it's
better to create a struct.

Why to use a structures?

 Use a struct when you need to store elements of different data types under one data
type.

 C++ structs are a value type rather than being a reference type. Use a struct if you don't intend
to modify your data after creation

C++ Structure Initialization

To create a C++ structure, we use the struct keyword, followed by an identifier. The identifier
becomes the name of the struct. Here is the syntax for creation of a C++ struct:

Syntax:

struct struct_name
{
// struct members
};

In the above syntax, we have used the struct keyword. The struct_name is the name of the
structure.The struct members are added within curly braces. These members probably belong to
different data types.

For example:

struct Person
{
char name[30];
int citizenship;
int age;
}

In the above example, Person is a structure with three members. The members include name,
citizenship, and age. One member is of char data type, while the remaining 2 are integers when a
structure is created, memory is not allocated. Memory is only allocated after a variable is added to the
struct.
Creating Struct Instances

In the above example, we have created a struct named Person. We can create a struct variable as
follows:

Person p;

The p is a struct variable of type Person. We can use this variable to access the members of the
struct.

Accessing Struct Members

To access the struct members, we use the instance of the struct and the dot (.) operator. For
example, to access the member age of struct Person:

p.age = 27;

We have accessed the member age of struct Person using the struct's instance, p. We have then set
the value of the member age to 27.

#include <iostream>
using namespace std;
struct Person
{
int citizenship;
int age;
};
int main(void) {
struct Person p;
p.citizenship = 1;
p.age = 27;
cout << "Person citizenship: " << p.citizenship << endl;
cout << "Person age: " << p.age << endl;
return 0;
}

Struct as Function Argument

You can pass a struct to a function as an argument. This is done in the same way as passing a
normal argument. The struct variables can also be passed to a function. A good example is when you
need to display the values of struct members. Let's demonstrates this:

#include<iostream>
using namespace std;

struct Person
{
int citizenship;
int age;
};
void func(struct Person p);

int main()
{
struct Person p;

p.citizenship = 1;
p.age = 27;

func(p);
return 0;
}
void func(struct Person p)
{
cout << " Person citizenship: " << p.citizenship<<endl;
cout << " Person age: " << p.age;
}

Pointers to Structure and passing structure instance pointer to function

It is possible to create a pointer that points to a structure. It is similar to how pointers pointing to
native data types like int, float, double, etc are created. Note that a pointer in C++ will store a memory
location address.

Example

#include <iostream>
using namespace std;

struct Length
{
int meters;
float centimeters;
};
int main()
{ Length *ptr, l;
ptr = &l;

cout << "Enter meters: ";


cin >> (*ptr).meters;
cout << "Enter centimeters: ";
cin >> (*ptr).centimeters;
cout << "Length = " << (*ptr).meters << " meters " << (*ptr).centimeters << " centimeters";
return 0;
}
Example

This example demonstrates the use of pointers defined as an instance of a structure, passed to a
function.

#include<iostream>
using namespace std;
#include<cstring>

struct talha
{
int age;
string fullname;
float height;
};

void display(talha *x)


{
cout<<(*x).age<<endl;
cout<<(*x).height<<endl;
cout<<(*x).fullname;
}

int main()
{

talha *ptr,p;
ptr=&p;
(*ptr).age=29;
(*ptr).height=5.11;
(*ptr).fullname="talhaakmalahmed";
display(ptr);
cout<<(*ptr).age;

Array of structures

In programming, an array is a collection of elements of same data type. Structure is a user defined
data type. So, we make the array of Structures, to store the multiple records, where each structure is
stored on the specific index in array. Each structure is accessed through array index.
Example

#include<iostream>
using namespace std;
#include<cstring>
struct human
{
int age;
string fullname;
float height;
};

int main()
{

human arr[2];
arr[0].age=27;
arr[0].height=5.8;
arr[0].fullname="shahidafridi";

arr[1].age=28;
arr[1].height=5.9;
arr[1].fullname="shoib malik";

cout<<endl;
cout<<endl;

cout<<arr[0].age<<endl;
cout<<arr[0].height<<endl;
cout<<arr[0].fullname<<endl;

cout<<arr[1].age<<endl;
cout<<arr[1].height<<endl;
cout<<arr[1].fullname<<endl;

LAB TASK

You might also like