Chapter 6 Structure
Chapter 6 Structure
Chapter Six
simple structured
pointer reference
03/27/2025 Fundamental of Programming II 3
Introduction To Structure
A structure is a collection of variables referenced under one name, providing a
convenient means of keeping related information together.
In contrast, array is homogeneous, it can contain only data of the same type.
A structure declaration forms a template that may be used to create structure
objects (that is, instances of a structure).
The variables that make up the structure are called members.
• Structure members are also commonly referred to as elements or fields.
• Example:
struct Date {
int day; The “Date” structure has 3
int month;
int year; members, day, month &
} ; year.
struct StatisticsType
{ float failRate ;
DateType lastServiced ; // DateType is a struct type
int downDays ;
};
struct MachineRec
{ int idNumber ;
string description ;
StatisticsType history ; // StatisticsType is a struct type
DateType purchaseDate ;
float cost ;
};
MachineRec machine ;
03/27/2025 Fundamental of Programming II 19
Nested structures…
• Examples 2:
struct point{
double x, y;
};
point P;
line
struct line{
point p1, p2; p1 p2
}; x y x y
line L;
struct triangle{
point p1, p2, p3;
};
triangle T;
03/27/2025 Fundamental of Programming II 20
Example 3
struct Example1
{
char x;
int y;
float z;
char s[10];
} ex;
p->balance
• The−>is usually called the arrow operator, and consists of the
minus sign followed by a greater-than sign.
• The arrow is used in place of the dot operator when you are
accessing a structure member through a pointer to the structure.
• Reading assignment
– Enumeration user define data type