Ece532 Chapter 1.3 Structure
Ece532 Chapter 1.3 Structure
Ece532 Chapter 1.3 Structure
CHAPTER 1: REVISION OF
COMPUTER PROGRAMMING
CONCEPT: STRUCTURE
Lecturer: Dr. Roslina Mohamad
Room: Tower 2, Level 13, No:14C
1
2
Learning Outcomes
At the end of this lecture, you will be able to:
Understand about records (structs)
Examine various operations on a struct
Explore ways to manipulate data using a
struct
Discover how arrays and pointers are used
in a struct
3
Structures
Structure: Programmer-defined data type that
allows multiple variables to be grouped together
Members may be of different types
Structure Declaration Format:
struct structure name
{
type1 field1;
type2 field2;
…
typen fieldn;
};
4
Example struct Declaration
struct Student
{ structure name
int studentID;
string name; structure members
short year;
double gpa;
};
Notice the
required ;
5
(structs)
• A struct is a definition, not a declaration
6
7
(structs)
Defining Structure Variables
O struct declaration does not allocate
memory or create variables
O To define variables, use structure tag as type
name
Student_UITM s1; s1
studentID
name
year
gpa
8
Accessing struct Members
The syntax for accessing a struct member is:
9
Input/Output
• No aggregate input/output operations on a struct
variable
• Data in a struct variable must be read one member
at a time
• The contents of a struct variable must be written
one member at a time
10
DISPLAYING STRUCT MEMBERS
To display the contents of a struct variable, you must
display each field separately, using the dot operator
Wrong:
cout << s1; // won’t work!
Correct:
cout << s1.studentID << endl;
cout << s1.name << endl;
cout << s1.year << endl;
cout << s1.gpa;
11
12
Assignment
Value of one struct variable can be assigned to
another struct variable of the same type using
an assignment statement
The statement:
student = newStudent;
copies the contents of newStudent into
student
Assignment
The assignment statement:
student = newStudent;
13
ACCESSING STRUCT MEMBERS
More examples:
cin >> newStudent.firstName;
cin >> newStudent.testScore >>
newStudent.programmingScore;
score = (newStudent.testScore +
newStudent.programmingScore) / 2;
14
Accessing struct Members
15
Accessing struct Members
To initialize the members of newStudent:
newStudent.GPA = 0.0;
newStudent.firstName = "John";
newStudent.lastName = "Brown";
16
Example
• Example 1.13
• Example 1.14
17
Comparing struct Members
• Similar to displaying a struct, you cannot compare two struct
variables directly:
18
19
Initializing a Structure
Cannot initialize members in the structure declaration, because
no memory has been allocated yet
20
Using an Initialization List
An initialization list is an ordered set of values, separated
by commas and contained in { }, that provides initial
values for a set of data members
21
Initialization List Example
Structure Declaration Structure Variable
height; width 6
};
height 3
Dimensions box = {12,6,3};
Nested Structures
A structure can have another structure as a
member.
struct PersonInfo
{ string name,
address,
city;
};
struct Student
{ int studentID;
PersonInfo pData;
short year;
double gpa;
};
Members of Nested Structures
24
structs within a struct
versus
25
Example structs within a struct
//See Example 1.16
26
Arrays versus structs
27
Arrays in structs
• Two key items are associated with a list:
• Values (elements)
• Length of the list
• Define a struct containing both items:
28
Arrays in structs
29
Arrays in structs
30
structs in Arrays
31
structs in Arrays
32
structs in Arrays
33
Example structs in Arrays
34
Pointers to Structures
Can create pointers to objects and structure
variables
struct Student {…};
Student stu1;
Student *stuPtr = &stu1;
35
Structure Pointer Operator
Simpler notation than (*ptr).member
Use the form ptr->member:
stuPtr->studentID = 12204;
squarePtr->setSide(14);
36
Example of Pointers to Structures
37