Professional Documents
Culture Documents
Lecture 11
Lecture 11
Example:
struct Product
{
int ID;
string name;
float price;
};
Structure Definition Syntax
Declaring a Structure variable
• Structure variable can be defined after the definition of a
structure. The syntax of the declaration is:
StructName Identifier;
Example:
part processor;
part keyboard;
4 Bytes
modelnumber
struct part
{ 4 Bytes
int partnumber
modelnumber;
int partnumber;
float cost; 4 Bytes
}; cost
Another way of Declaring a Structure variable
• You can also declare struct variables when you
define the struct. For example:
struct part
{
int modelnumber;
int partnumber;
float cost;
} part1;
employee e1;
Initializing Structure Variables
• The syntax of initializing structure is:
void main( )
{
student BC022010= {“M”, “Umar”, ‘A’, 94} ;
}
student BC111017;
BC111017.firstName = “Muhammad”;
BC111017.lastName = “Umar”;
BC111017.courseGrade = ‘A’;
BC111017.marks = 93;
Assigning Values to Structure Variables
• After creating structure variable, values to structure
members can be assigned using cin.
student BC012311;
cin>>BC012311.firstName;
cin>>BC012311.lastName;
cin>>BC012311.courseGrade;
cin>>BC012311.marks ;
Assigning one Structure Variable to another
• A structure variable can be assigned to another
structure variable only if both are of same type
Example:
studentType newStudent = {“John”, “Lee”, ‘A’, 99} ;
struct Book
{
int ID;
int Pages;
float Price;
};
CUSTLibrary[0]
ID Pages Price
CUSTLibrary[1]
ID Pages Price
… CUSTLibrary[99]
ID Pages Price
Initialization of Array of Structures
struct Book
{
int ID;
int Pages;
float Price;
};
Book b[3]; // declaration of array of structures
struct Student
{
int RollNo;
float Marks[3];
};
struct B
{ record
char ch; v1
A v1; ch x y
};
B record;
Initializing/Assigning to Nested Structure
struct A{ void main()
int x; {
float y; B record;
}; cin>>record.ch;
cin>>record.v2.x;
struct B{ cin>>record.v2.y;
char ch; }
A v2;
}; void main()
{
B record;
void main() record.ch = ‘S’;
{ record.v2.x = 100;
B record = {‘S’, {100, 3.6} }; record.v2.y = 3.6;
} }
Passing structures and array of structures to
functions by pointers
int main(){
struct student s;
s.roll_no = 4;
s.name = "Ron";
s.phone_number = 888888;
display(s);
cout<<s.phone_number;
return 0;
Passing by Value
Passing by Value
• In the above example, we are printing roll number, name
and phone number of a student using a function. We first
declared a structure named student with roll_no, name
and phone number as its members and 's' as its variable.
Then we assigned the values of roll number, name and
phone number to the structure variable s. Just as we pass
any other variable to a function, we passed the structure
variable 's' to a function 'display'.
• Now, while defining the function, we passed a copy of the
variable 's' as its argument with 'struct student' written
before it because the variable which we have passed is of
type structure named student. Finally, in the function, we
printed the name, roll number and phone number of the
Passing by Reference
In passing by reference, the address of a structure variable is
passed to a function. In this, if we change the structure
variable which is inside the function, the original structure
variable which is used for calling the function changes. This
was not the case in calling by value.
struct student
{
int roll_no;
string name;
int phone_number;
};
void display(struct student &st)
{
cout << "Roll no : " << st.roll_no << endl;
cout << "Name : " << st.name << endl;
cout << "Phone no : " << st.phone_number << endl;
st.phone_number=4343;;
}
int main(){
struct student s;
s.roll_no = 4;
s.name = "Ron";
s.phone_number = 888888;
display(s);
cout<<s.phone_number;
return 0;
}
Passing by Reference
Passing by Reference
• This case is similar to the previous one, the only difference
is that this time, we are passing the address of the
If you do
structure not practice,
variable you don't
to the function. Whiledeserve
declaringto
thewin.
function, we passed the pointer of the copy 'st' of the
structure variable 's' in its parameter. Since-Andre Agassi
the pointer is
of a variable of type structure named student, we wrote
'struct student' before the name of the pointer in the
argument of the function. In the function , we accessed
If you do not practice, you don't deserve to pass.
the members of the pointer using -> sign as discussed
before.
• Try to change the value of the variables-Ghulam
inside theMustafa
function in both the cases and see the changes in the real
variables.
Class Exercises(1) – Find Errors
• Find errors:
struct
{
int x;
float y;
};
struct values
{
char name[20];
int age;
}
Class Exercises(2) – Find Errors
• Find errors:
struct TwoVals
{
int a,b;
};
int main()
{
TwoVals.a=10;
TwoVals.b=20;
}
Class Exercises(3) – Find Errors
• Find errors:
struct ThreeVals
{
int a,b,c;
};
int main()
{
ThreeVals vals={1,2,3};
cout<<vals<<endl;
return 0;
}
Class Exercises(4) – Find Errors
• Find errors:
struct names
{
char first[20];
char last[20];
};
int main()
{
names customer = {“Muhammad”, “Ali”};
cout<<names.first<<endl;
cout<<names.last<<endl;
return 0;
}
Class Exercises(5) – Find Errors
• Find errors:
struct TwoVals
{
int a=5;
int b=10;
};
int main()
{
TwoVals v;
cout<<v.a<<“”<<v.b;
return 0;
}
Class Exercise-6
• Define a structure called “car”. The member elements
of the car structure are:
• string Model;
• int Year;
• float Price
PhoneDirectory
City Country
Class Exercise-7
Study After Pointer
Topic
Accessing Structures with Pointers
• Pointer variables can be used to point to structure
type variables too.
• The pointer variable should be of same type, for
example: structure type
struct Rectangle {
int width;
int height;
};
void main( )
{
Rectangle rect1={22,33};
Rectangle* rect1Ptr = &rect1;
}
Accessing Structures with Pointers
• How to access the structure members (using
pointer)?
– Use dereferencing operator (*) with dot (.) operator
struct Rectangle {
int width;
int height;
};
void main( )
{
Rectangle rect1={22,33};
Rectangle* rectPtr = &rect1;
cout<< (*rectPtr).width<<(*rectPtr).height;
}
Accessing Structures with Pointers
• Is there some easier way also?
– Use arrow operator ( -> )
struct Rectangle {
int width;
int height;
};
void main( )
{
Rectangle rect1={22,33};
Rectangle* rectPtr = &rect1;
cout<< rectPrt->width<<rectPrt->height;
}
Passing by Reference