03 Lecture Three
03 Lecture Three
Review on Structures
2
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.
3
Structure Syntax
Example
Structure Syntax A structure of a company’s inventory
4
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
5
Structure Syntax
};
7
Accessing Structure
Members
(Dot
inv_type x, y; item Operator)
x cost retail
strcpy(x.item, “Red Pen”); on_hand load_time
x.cost= 5.99;
cout<<x.cost;
cin>>x.retail; item
8
Example
struct movies_t
{
string title;
int year;
} mine, yours;
void printmovie
(movies_t movie);
int main ()
{
string mystr;
mine.title =
"2001 A Space
9
Odyssey";
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: "; 2001 A Space Odyssey (1968)
cin.getline (yours.title); And yours is:
cout << "Enter year: ";
cin.getline (yours.year);
Alien (1979)
void printmovie
(movies_t movie)
{
cout <<
movie.title;
cout << " (" <<
movie.year << ")\ 10
n";
Arrays of Structures
cost retail
x[0] 25
on_hand load_time
item
item
item
item
cost retail
x[4] -147
on_hand load_time
#define N_MOVIES 3
struct movies_t
{
string title;
int year;
} films [N_MOVIES];
void printmovie
int main ()
(movies_t movie);
{
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) >> 15
films[n].year;
Example (cont.)
...
Enter title: Blade Runner
cout << "\nYou have entered these movies:\n"; Enter year: 1982
for (n=0; n<N_MOVIES; n++) Enter title: Matrix
printmovie (films[n]);
Enter year: 1999
return 0;
} Enter title: Taxi Driver
Enter year: 1976
void printmovie (movies_t
movie)
{ You have entered these movies:
cout << movie.title; Blade Runner (1982)
cout << " (" << Matrix (1999)
movie.year << ")\n"; 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
()
{ 17
struct sample x;
x.a=1000;
Assigning Structures
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;
struct stype
{
Structures
int num[10][10];
float b;
} var;
18
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;
};
19
20