By
Dr. Ahmed Taha
Lecturer, Computer Science Department,
Faculty of Computers & Artificial Intelligence,
Benha University Lecture 3
1
Lecture Three
Review on Structures
2
3
4
Structure Definition
• A structure is a group of related variables.
• It is a set of data elements grouped together under
one name.
• These data elements, known as members, can have
different types and different lengths.
• Structures are called compound data types because
they consists of several different data types.
5
Structure Syntax
struct structure_name struct inv_type
{ {
member_type1 member_name1; char item [40]; //item name
member_type2 member_name2; double cost; //cost
member_type3 member_name3; double retail; //retail price
. int on_hand; //amount on hand
. int load_time; //number of days before
} variable_names; //resupply
} X, y, z;
Structure Syntax Example
A structure of a company’s inventory
6
Structure Syntax
• The declaration of the structure is terminated by a
semicolon (;). This is because a structure declaration
is a statement.
• No variable has actually been created. Only the
compiler knows the form of the data.
• When the compiler allocates variables?
– When you define a variable of that structure
7
Structure Syntax
• To declare a structure variable whenever it is needed,
just use the structure name as a data type.
inv_type x, y, z;
• If you want to use only one structure variable. So, it
is not necessary to include the name of the structure.
struct
{
int a,b;
double c;
} x; 8
How Structure looks
like in memory?
• If the size of int is 4 bytes , double is 8 bytes, and
char is one byte: struct inv_type
{
char item [40]; // 40 Bytes
item double cost; // 8 Bytes
double retail; // 8 Bytes 64 bytes
cost retail int on_hand; // 4 Bytes
on_hand load_time int load_time; // 4 Bytes
};
9
Accessing Structure
Members
(Dot Operator)
inv_type x, y; item
x
cost retail
strcpy(x.item, “Red Pen”);
x.cost= 5.99;
on_hand load_time
cout<<x.cost;
cin>>x.retail; item
strcpy(y.item, “Pencil”); y
cost retail
y.cost= 15.5;
cout<<y.cost; on_hand load_time
cin>>y.retail;
10
Example
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t
{
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2001 A Space Odyssey"; mine.year = 1968;
...
11
Example (cont.)
...
Enter title: Alien
int main () Enter year: 1979
{
mine.title = "2001 A Space Odyssey"; mine.year = 1968;
My favorite movie is:
cout << "Enter title: ";
cin.getline (yours.title); 2001 A Space Odyssey (1968)
cout << "Enter year: "; And yours is:
cin.getline (yours.year);
Alien (1979)
cout << "My favorite movie is:\n "; printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}
void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}
12
Arrays of Structures
• To declare an array of structures, you must first
define a structure, then declare an array of its type.
inv_type x[10];
• To print the on_hand member of the third structure,
you should write
cout<<x[2].on_hand;
13
Arrays of Structures
item
cost retail
x[0] 25
on_hand load_time
item
cost retail
x[1] -3
on_hand load_time
item
cost retail
x[2] 312
on_hand load_time
item
cost retail
x[3] 89
on_hand load_time
item
cost retail
x[4] -147
on_hand load_time
inv_type x[10]; int x[10]; 14
// array of structures
#include <iostream>
Example
#include <string>
#include <sstream>
using namespace std;
#define N_MOVIES 3
struct movies_t
{
string title; int year;
} films [N_MOVIES];
void printmovie (movies_t movie);
int main ()
{
string mystr;
int n;
for (n=0; n<N_MOVIES; n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}
... 15
Example (cont.)
...
Enter title: Blade Runner
cout << "\nYou have entered these movies:\n"; Enter year: 1982
for (n=0; n<N_MOVIES; n++)
printmovie (films[n]); Enter title: Matrix
return 0; Enter year: 1999
}
Enter title: Taxi Driver
void printmovie (movies_t movie) Enter year: 1976
{
cout << movie.title;
cout << " (" << movie.year << ")\n"; You have entered these movies:
}
Blade Runner (1982)
Matrix (1999)
Taxi Driver (1976)
16
Passing a structure
to a function
// example about passing a structure to a function
#include <iostream> 1000
using namespace std;
struct sample
{
int a,b;
char ch;
};
void fun (sample x)
{
cout<<x.a;
}
void main ()
{
struct sample x;
x.a=1000;
fun(x);
}
17
Assigning Structures
// example about assigning two structures
#include <iostream> Ahmed
#include <string> 23
using namespace std;
A
struct student
{
char name[100];
int id;
char std_class;
};
void main ()
{
student std1,std2;
std1.id=23;
strcpy(std1.name,”ahmed”);
std1.std_class=‘a’;
std2=std1;
cout<<std2.name<<endl;
cout<<std2.id<endl;
cout<<std2.std_class;
} 18
Pointer to Structures
struct bal
{
float balance;
char name [80];
} person;
bal *p; // declare a structure pointer
p=&person; // puts the address of person into pointer p
• To access the members, you must use arrow operator
(->) instead of dot operator (.)
p->balance=0.1;
19
Arrays within
Structures
struct stype
{
int num[10][10];
float b;
} var;
• To access the element num at index 3,7 in that array
var.num[3][7]=5;
20
Nested Structures
struct address
{
char name[50];
char street[50];
char city[50];
char zip[50];
};
struct employee
{
char emp_name[50];
float emp_salary;
address emp_address;
};
21
22