Structures: Fahad Majeed UET Taxila
Structures: Fahad Majeed UET Taxila
Fahad Majeed
UET Taxila
2
Programming
Structures
Structures
3
Structures
• Structures hold data that belong together.
• 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.
4
Structures
• Individual components of a struct type are called
members (or fields).
• Members can be of different types (simple, array
or struct).
• A struct is named as a whole while individual
members are named using field identifiers.
• Complex data structures can be formed by
defining arrays of structs.
5
struct basics
• Definition of a structure:
struct <struct-type>{
<type> <identifier_list>; Each identifier
<type> <identifier_list>; defines a member
...
of the structure.
} ;
• Example:
struct Date {
int day; The “Date” structure
int month; has 3 members,
int year;
} ; day, month & year.
6
struct examples
• Example:
struct StudentInfo{
int Id;
int age; The “StudentInfo”
char Gender; structure has 4 members
double CGA;
}; of different types.
• Example:
struct StudentGrade{
char Name[15];
char Course[9]; The “StudentGrade”
int Lab[5];
int Homework[3]; structure has 5
int Exam[2]; members of
};
different array types.
7
struct examples
• Example:
struct BankAccount{
char Name[15]; The “BankAcount”
int AcountNo[10]; structure has simple,
double balance; array and structure
Date Birthday;
types as members.
};
• Example:
struct StudentRecord{ The “StudentRecord”
char Name[15]; structure has 4
int Id;
char Dept[5]; members.
char Gender;
}; 8
Program to assign data to members of a
structure variable and display it.
#include <iostream>
using namespace std;
struct Person
{
char name[50];
int age;
float salary;
};
int main()
{
Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary; return 0;
}
OUTPUT
Displaying Information.
Name: XYZ JKL
Age: 27
Salary: 1024.4
Example: 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; }
OUTPUT
Enter information,
Enter name: XYZ
Enter roll number: 4
Enter marks: 55.6
Displaying Information,
Name: XYZ
Roll: 4
Marks: 55.6
struct basics
• Declaration of a variable of struct type:
<struct-type> <identifier_list>;
• Example:
StudentRecord Student1, Student2;
Dept Dept
13
struct basics
• The members of a struct type variable are accessed
with the dot (.) operator:
<struct-variable>.<member_name>; Student1
Name
• Example:
strcpy(Student1.Name, "Chan Tai Man"); Id Gender
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP"); Dept
Student1.gender = 'M';
cout << "The student is ";
switch (Student1.gender){
case 'F': cout << "Ms. "; break; Chan Tai Man
case 'M': cout << "Mr. "; break;
}
cout << Student1.Name << endl; 12345 M
COMP
14
15
struct-to-struct assignment
• The values contained in one struct type variable can be
assigned to another variable of the same struct type.
Student1
• Example:
Chan Tai Man
strcpy(Student1.Name,
"Chan Tai Man"); 12345 M
Student1.Id = 12345;
strcpy(Student1.Dept, "COMP");
Student1.gender = 'M'; COMP
Student2 = Student1;
12345 M
Student2
16 COMP
17
Nested structures
• We can nest
structures inside
structures.
(P.x, P.y)
• struct line{
point p1, p2; (L.p2.x, L.p2.y)
};
line L; (L.p1.x, L.p1.y)
line
p1 p2
x y x y
19
Arrays of structures
• An ordinary array: One type of data
0 1 2 … 98 99
0 1 2 … 98 99
20
Arrays of structures
• We often use arrays of structures.
• Example:
StudentRecord Class[100];
strcpy(Class[98].Name, "Chan Tai Man");
Class[98].Id = 12345;
strcpy(Class[98].Dept, "COMP");
Class[98].gender = 'M';
Class[0] = Class[98]; Chan Tai Man
12345 M
COMP
...
0 1 2 … 21 98 99
Arrays inside structures
• We can use arrays inside structures.
• Example: (4, 3) (10, 3)
struct square{
point vertex[4];
};
square Sq; (4, 1) (10, 1)
x y x y x y x y
22
Reference
• https://www.youtube.com/watch?v=-IrueTrxNHA