0% found this document useful (0 votes)
3 views

03 Lecture Three

Uploaded by

nouryones38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

03 Lecture Three

Uploaded by

nouryones38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

By

Dr. Eman Monir


Lecturer, Computer Science Department,
Faculty of Computers & Artificial Intelligence,
Benha University Lecture 3
1
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

struct structure_name struct inv_type


{ {
member_type1 member_name1; char item [40]; //item name
member_type2 double cost; //cost
member_name2; double retail; //retail price
member_type3 member_name3; int on_hand; //amount on hand
. int load_time; //number of days before
. //resupply
} variable_names; } X, y, z;

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

• 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 Structurelooks
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

};

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

strcpy(y.item, “Pencil”); y cost retail


y.cost= 15.5;
cout<<y.cost; on_hand load_time
cin>>y.retail;

8
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
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)

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 << ")\ 10
n";
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;
11
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
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

// 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;
17
Arrays within

struct stype
{
Structures
int num[10][10];
float b;
} var;

• To access the element num at index 3,7 in that array


var.num[3][7]=5;

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

You might also like