Structure
Structure
1
structures
• Student is an object which contain attributes like rollno, name,
department, course, etc.
– All these attributes of different types.
• How to store such an information under same name ?
– structures
• A Structure is a collection of related data items, possibly of
different types.
• A structure type in C++ is called struct.
• A struct is heterogeneous in that it can be composed of data of
different types.
• In contrast, array is homogeneous since it can contain only
data of the same type.
2
structures
• Examples:
– Student record: student id, name, major, gender,
start year, …
– Bank account: account number, name, currency,
balance, …
– Address book: name, address, telephone number,
…
• In database applications, structures are called records.
3
structures
• Individual components of a struct type are called members (or
fields).
• Members can be of different types (simple, array or struct).
struct student
{ int rollno;
char name[20]; Members of the
char department[30]; struct
char course[20];
}s1, s2, s3;
where student is a structure name // like int (int is a primitive data type)
4
Declaring struct variables
struct student p, q, r;
• Declares and sets aside storage for three variables – p, q,
and r – each of type struct student.
7
Example
• Consider the problem discussed in the previous class and do the
solution using structures.
#include<iostream>
using namespace std;
struct student
{ int rollno;
int marks;
};
student s1[60], s2[60], s3[60];
struct total
{ int rollno;
int m1,m2, m3,sum;
char grade; }t[60];
8
Example
int main()
{ int n;
cout << "Enter number of students:"; cin >> n;
// read the data into s1, s2, s3
int i=0;
// get the data into student structure
while(i< n)
{ cout << "\n Enter the " << i+1 << "student details";
cin >> s1[i].rollno >> s1[i].marks >> s2[i].marks >> s3[i].marks;
s2[i].rollno = s3[i].rollno = s1[i].rollno;
i++;
}
9
Example
// read the data from the student and write it into total
i=0;
while(i<=n) // Include the marks validation
{ t[i].rollno = s1[i].rollno;
t[i].m1 = s1[i].marks;
t[i].m2 = s2[i].marks;
t[i].m3 = s3[i].marks;
t[i].sum = t[i].m1 + t[i].m2 + t[i].m3;
if (t[i].m1 >= 40 && t[i].m2 >= 40 && t[i].m3 >= 40)
t[i].grade = 'P';
else t[i].grade = 'F';
i++;
}
10
Example
// display the data
i=0;
while(i<n)
{
cout << endl << t[i].rollno << " " << t[i].m1 << " " << t[i].m2 ;
cout << " " << t[i].m3 << " " ;t[i].sum << t[i].grade;
i++;
}
return 0;
}
11
How to pass or return a structure
to/from a Function
Passing of structure to the function can be done
in two ways:
• By passing all the elements to the function
individually.
• By passing the entire structure to the function.
12
#include <iostream>
using namespace std; Using Call By Value
struct Distance { Method
int kilometer;
int meter;
};
Distance2.kilometer = 9;
Distance2.meter = 745;
14
// Driver code0
int main()
{
return 0;
}
15
Using Call By reference
Method
/ C++ program to pass structure as an argument
// to the functions using Call By Reference Method
#include <bits/stdc++.h>
using namespace std;
struct number {
int n;
};
16
void initializeFunction()
{
number n1;
// assigning value to n
n1.n = 10;
return 0;
}
18
How to return a structure from the
functions?
// C++ program to return a structure from
// a function using Call By Value Method
#include <iostream>
#include <stdlib.h>
// required structure
struct Employee {
int Id;
string Name;
};
19
/ return type of the function is structure
Employee data(Employee E)
{
// returning structure
return (E);
}
20
// Driver code
int main()
{
return 0;
}
21
C++ – Pointer to Structure
• Pointer to structure in C++ can also be
referred to as Structure Pointer. A structure
Pointer in C++ is defined as the pointer which
points to the address of the memory block
that stores a structure.
• Syntax:
ptr = &structure_variable;
22
#include <iostream>
23
// Function to calculate area of
// the given Square
void area_Square(struct square s)
{
// Find the area of the Square
// using variables of point
// structure where variables of
// point structure is accessed
// by left and right objects
int area = (s.right.x) * (s.left.x);
// Driver Code
int main()
{
// Initialize variable 's'
// with vertices of Square
struct square s = { { 4, 4 }, { 4, 4 } };
// Function Call
area_Square(s);
return 0;
} 24