Lec 4
Lec 4
Structures:
A structure is a collection of simple variables. The variables in a structure can be
of different types: int, float, and so on. (This is unlike the array, in which all the
variables must be the same type.) The data items in a structure are called the
members of the structure.
struct Name
{
type Element1;
type Element2;
type Element3;
.
.
} Object_Name;
Examples:
1
Data Structure Lecture-4
struct student
{
char name [20];
int age;
};
void main()
{
student st1, st2;
}
The second way the declaration is after the end of the structure body as shown in the
example:
struct student
{
char name [20];
int age;
} st1, st2;
Example: Design a structure named (student) contains three elements (name, stage and degree):
2
Data Structure Lecture-4
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int stage;
int mark;
};
int _tmain(int argc, _TCHAR* argv[])
{
student std1, std2;
cout << "Enter the information of the first student: "<<"\n \n";
cin >> std1.name;
cin >> std1.stage;
cin >> std1.mark;
cout << "Enter the name of the second student" << "\n\n";
cin >> std2.name;
cin >> std2.stage;
cin >> std2.mark;
cout << "The information of first student is: " << std1.name << " " << std1.stage << " " << std1.mark << "\n\n";
cout << "The information of second student is: " << std2.name << " " << std2.stage << " " << std2.mark << "\n\n";
return 0;
}
3
Data Structure Lecture-4
Example: Design a structure named (student) contains three elements (string name, integer stage and
integer mark [3]):
Write a program to execute the followings:
1- Read all three information for 2 students. 2- Print all information for 2 students.
struct student
{
string name;
int stage;
int mark[3];
};
cout << "Enter the information of the first student: "<<"\n \n";
cin >> std1.name;
cin >> std1.stage;
cout << "Enter the name of the second student" << "\n\n";
cin >> std2.name;
cin >> std2.stage;
cout << "The information of first student is: " << std1.name << " " << std1.stage << " ";
for (i = 0; i < 3; i++)
cout<< std1.mark[i] << " ";
cout << endl;
cout << "The information of second student is:" << std2.name << " " << std2.stage << " ";
for (i = 0; i < 3; i++)
cout << std2.mark[i] << " ";
return 0;
}
4
Data Structure Lecture-4
struct student
{
string name;
int stage;
int mark[2];
}stdnt[3];
#include<string>
using namespace std;
struct student
{
string name;
int stage;
int mark[2];
} stdnt[3];
cout << "Enter the information of a new student: " << "\n \n";
cin >> stdnt[j].name;
cin >> stdnt[j].stage;
for (i = 0; i < 2; i++)
cin >> stdnt[j].mark[i];
}
return 0;
}
5
Data Structure Lecture-4
Simply saying: To access members of a structure, use the dot operator. To access
members of a structure through a pointer, use the arrow operator.
struct student
{
string name;
int stage;
int mark;
}*stdptr;
- To see how to access structure members, we can use a pointer to the student structure and
do the same instructions on the previous example.
- The first step after the declaration of the object and the pointer is to initialize the pointer with
the memory address of the object:
stdptr = &std1;
- After that we can access the structure members directly or via pointer.
- The output of the program will be:
6
Data Structure Lecture-4
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int stage;
int mark;
};
stdptr = &std1;
cout << "Enter the information of a new student: " << "\n \n";
stdptr->stage = stdptr->stage + 1;
stdptr->mark = 88;
return 0;
}
7
Data Structure Lecture-4
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int stage;
int mark[3];
};
cout << "Enter the information of a new student: " << "\n \n";
cin >> std1.name;
cin >> std1.stage;
for (i = 0; i < 3; i++)
cin >> std1.mark[i];
checkfun(&std1);
return 0;
}
sum +=stdpointer->mark[i];
}
avg = sum / 3;