Lecture 5
Lecture 5
1
Objectives
In this chapter, you will:
• Learn about records (structs)
• Examine various operations on a struct
• Explore ways to manipulate data using a
struct
• Learn about the relationship between a struct
and functions
• Discover how arrays are used in a struct
• Learn how to create an array of struct items
2
Records (structs)
• struct: collection of a fixed number of
components (members), accessed by name
• A struct is a definition, not a declaration
− Members may be of different types
• Syntax:
3
4
Accessing struct Members
5
Accessing struct Members
(continued)
• To initialize the members of newStudent:
newStudent.GPA = 0.0;
newStudent.firstName = "John";
newStudent.lastName = "Brown";
6
Accessing struct Members
(continued)
− More examples:
8
Assignment
9
Assignment (continued)
newStudent.programmingScore;
student.GPA = newStudent.GPA;
10
Example:
11
Comparison (Relational Operators)
12
Input/Output
13
struct Variables and Functions
• A struct variable can be passed as a
parameter by value or by reference
14
Arrays versus structs
15
Arrays in structs
• Two key items are associated with a list:
− Values (elements)
− Length of the list
• Define a struct containing both items:
16
17
Arrays in structs (cont'd.)
- 18
18
19
Prepared by: Malak Abdullah---- 20
structs within a struct
versus
21
22
Prepared by: Malak Abdullah---- 23
Summary
24
Summary (continued)