4-Structures e
4-Structures e
(COMP-112)
Example:
struct Product
{
int ID;
string name;
float price;
};
Structure Definition Syntax
Structure members in memory
Memory is only allocated
when we create struct type
variables
4 Bytes
modelnumber
struct part
{ 4 Bytes
int partnumber
modelnumber;
int partnumber;
float cost;
}; cost 4 Bytes
Declaring a Structure variable
• Structure variable can be created after the definition of
a structure. The syntax of the declaration is:
StructName Identifier;
Example:
part processor;
part keyboard;
Another way of Declaring a Structure variable
struct part
{
int
modelnumber;
int partnumber;
float cost;
} part1;
Employee e1;
Initializing Structure Variables
• The syntax of initializing structure is:
void main( )
{
Student s1= {“M”, “Umar”, ‘A’, 94} ;
}
s1.firstName = “Muhammad”;
s1.lastName = “Umar”;
s1.courseGrade = ‘A’;
s1.marks = 93;
Assigning Values to Structure Variables
• After creating structure variable, values to structure
members can be assigned using cin
student s1;
cin>>s1.firstName;
cin>>s1.lastName;
cin>>s1.courseGrade;
cin>>s1.marks ;
cout<<s1.firstName<<s1.lastName;
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 = {“Amir”, “Ali”, ‘A’, 98} ;
studentType student2 = newStudent;
Array of Structures
An array of structure is a type of array in which each
element contains a complete structure.
struct Book
{
int ID;
int Pages;
float Price;
};
Book Library[100]; // declaration of array of structures
ID
Library[0]
Pages Price ID
Library[1]
Pages Price
… ID
Library[99]
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
};
Initializing/Assigning to Nested Structure
struct A{ void main() // Input
int x; {
float y; B record;
cin>>record.ch;
}; cin>>record.v2.x;
cin>>record.v2.y;
struct B{ }
char ch;
A v2; void main() // Assignment
}; {
B record;
void main() // Initialization record.ch = ‘S’;
{ record.v2.x = 100;
B record = {‘S’, {100, 3.6} }; record.v2.y = 3.6;
} }
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<< (*rectPrt).width<<(*rectPrt).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;
}
Anonymous Structure
• Structures can be anonymous:
Other stuff you can do with a
struct
• You can also associate functions with a structure (called
member functions)
void print_ave( ) {
cout << "Name: " << name << endl;
cout << "Average: " << ave << endl;
}
};
Using the member function
StudentRecord stu;
stu.print_ave( );
Structures and Functions
• Structures can be passed in a function:
1. Pass-by-value
2. Pass-by-reference
3. Pass-by-reference (using pointers)
Structures and Functions – By Value
Structures and Functions – By Value
• Problem: copy of a large structure takes time and a lot
of memory
PhoneDirectory
City Country