Page 1
Computer Science: A Structured Programming Approach Using C 1
Objectives
❏ To introduce the structure, union, and enumerated types
❏ To use the type definition statement in programs
❏ To use enumerated types, including anonymous types.
❏ To create and use structures in programs
❏ To be able to use unions in programs
❏ To understand the software engineering concept of coupling and
to be able to evaluate coupling between functions.
Chapter 12
Enumerated, Structure,
and Union Types
Page 2
Computer Science: A Structured Programming Approach Using C 2
FIGURE 12-1 Derived Types
Page 3
Computer Science: A Structured Programming Approach Using C 3
12-1 The Type Definition (typedef)
Before discussing the derived types, let’s discuss a C
declaration that applies to all of them—the type
definition. A type definition, typedef, gives a name to a
data type by creating a new type that can then be used
anywhere a type is permitted.
Page 4
Computer Science: A Structured Programming Approach Using C 4
FIGURE 12-2 Type-definition Format
typedef int INGETER;
Page 5
Computer Science: A Structured Programming Approach Using C 5
12-2 Enumerated Types
The enumerated type is a user-defined type based on the
standard integer type. In an enumerated type, each
integer value is given an identifier called an enumeration
constant.
Declaring an Enumerated Type
Operations on Enumerated Types
Enumeration Type Conversion
Initializing Enumerated Constants
Anonymous Enumeration: Constants
Input/Output Operations
Topics discussed in this section:
Page 6
Declaring an Enumerated Type
enum typeName {identifier list};
enum color {RED, BLUE, GREEN, WHITE};
enum color skyColor;
Computer Science: A Structured Programming Approach Using C 6
Page 7
Operations on Enumerated Types enum color x, y;
x = BLUE; x = y;
if (color1 == color2)if (color1 == BLUE)
Switch (color1){case BLUE: …
Computer Science: A Structured Programming Approach Using C 7
Page 8
Enumeration Type Conversion
int x; enum color y;x = BLUE; y = 2;
enum color y;y = (enum color) 2;
Computer Science: A Structured Programming Approach Using C 8
Page 9
Initializing Enumerated Constants enum months {JAN, FEB, MAR, APR};
enum months {JAN = 1, FEB, MAR, APR};
enum color {RED, ROSE = 0, CRIMSON = 0, BLUE, AQUA = 1};
Computer Science: A Structured Programming Approach Using C 9
Page 10
Anonymous Enumeration: Constants enum {space = ‘ ‘, comma = ‘,’,
color = ‘:’};
enum {OFF, ON};
Computer Science: A Structured Programming Approach Using C 10
Page 11
Computer Science: A Structured Programming Approach Using C 11
PROGRAM 12-1 Print Cable TV Stations
Page 12
Computer Science: A Structured Programming Approach Using C 12
PROGRAM 12-1 Print Cable TV Stations
Page 13
Computer Science: A Structured Programming Approach Using C 13
Don’t be confused about strings and enumerated types.
“Jan” is a string made of three characters; JAN
as defined in the previous code example, is an
enumerated type (identifier) which
has the integer value 1.
Note
Page 14
Computer Science: A Structured Programming Approach Using C 14
12-3 Structure
A structure is a collection of related elements, possibly
of different types, having a single name.
Structure Type Declaration
Initialization
Accessing Structures
Operations on Structures
Complex Structures
Structures and Functions
Topics discussed in this section:
Page 15
Computer Science: A Structured Programming Approach Using C 15
FIGURE 12-3 Structure Examples
Page 16
Computer Science: A Structured Programming Approach Using C 16
Elements in a structure can be of the same or different types.
However, all elements in the structure
should be logically related.
Note
Page 17
Computer Science: A Structured Programming Approach Using C 17
FIGURE 12-4 Tagged Structure Format
Page 18
Computer Science: A Structured Programming Approach Using C 18
FIGURE 12-5 Structure Declaration with typedef
Page 19
Computer Science: A Structured Programming Approach Using C 19
FIGURE 12-6 Structure Declaration Format and Example
Page 20
Computer Science: A Structured Programming Approach Using C 20
FIGURE 12-7 Initializing Structures
Page 21
Computer Science: A Structured Programming Approach Using C 21
FIGURE 12-8 Structure Direct Selection Operator
Page 22
Computer Science: A Structured Programming Approach Using C 22
PROGRAM 12-2 Multiply Fractions
Page 23
Computer Science: A Structured Programming Approach Using C 23
PROGRAM 12-2 Multiply Fractions
Page 24
Computer Science: A Structured Programming Approach Using C 24
FIGURE 12-9 Copying a Structure
Page 25
Computer Science: A Structured Programming Approach Using C 25
FIGURE 12-10 Pointers to Structures
Page 26
Computer Science: A Structured Programming Approach Using C 26
(*pointerName).fieldName pointerName->fieldName.
Note
Page 27
Computer Science: A Structured Programming Approach Using C 27
FIGURE 12-11 Interpretation of Invalid Pointer Use
Page 28
Computer Science: A Structured Programming Approach Using C 28
FIGURE 12-12 Indirect Selection Operator
Page 29
Computer Science: A Structured Programming Approach Using C 29
PROGRAM 12-3 Clock Simulation with Pointers
Page 30
Computer Science: A Structured Programming Approach Using C 30
PROGRAM 12-3 Clock Simulation with Pointers
Page 31
Computer Science: A Structured Programming Approach Using C 31
PROGRAM 12-3 Clock Simulation with Pointers
Page 32
Computer Science: A Structured Programming Approach Using C 32
PROGRAM 12-3 Clock Simulation with Pointers
Page 33
Computer Science: A Structured Programming Approach Using C 33
FIGURE 12-13 Nested Structure
Page 34
Computer Science: A Structured Programming Approach Using C 34
FIGURE 12-14 Arrays in Structures
Page 35
Computer Science: A Structured Programming Approach Using C 35
FIGURE 12-15 Pointers in Structures
Page 36
Computer Science: A Structured Programming Approach Using C 36
FIGURE 12-16 Array of Structures
Page 37
Computer Science: A Structured Programming Approach Using C 37
PROGRAM 12-4 Sort Array of Student Structures
Page 38
Computer Science: A Structured Programming Approach Using C 38
PROGRAM 12-4 Sort Array of Student Structures
Page 39
Computer Science: A Structured Programming Approach Using C 39
PROGRAM 12-4 Sort Array of Student Structures
Page 40
Computer Science: A Structured Programming Approach Using C 40
PROGRAM 12-4 Sort Array of Student Structures
Page 41
Computer Science: A Structured Programming Approach Using C 41
PROGRAM 12-4 Sort Array of Student Structures
Page 42
Computer Science: A Structured Programming Approach Using C 42
PROGRAM 12-4 Sort Array of Student Structures
Page 43
Computer Science: A Structured Programming Approach Using C 43
FIGURE 12-17 Passing Structure Members to Functions
Page 44
Computer Science: A Structured Programming Approach Using C 44
FIGURE 12-18 Passing and returning structures
Page 45
Computer Science: A Structured Programming Approach Using C 45
PROGRAM 12-5 Passing and Returning Structures
Page 46
Computer Science: A Structured Programming Approach Using C 46
PROGRAM 12-5 Passing and Returning Structures
Page 47
Computer Science: A Structured Programming Approach Using C 47
PROGRAM 12-5 Passing and Returning Structures
Page 48
Computer Science: A Structured Programming Approach Using C 48
PROGRAM 12-5 Passing and Returning Structures
Page 49
Computer Science: A Structured Programming Approach Using C 49
PROGRAM 12-5 Passing and Returning Structures
Page 50
Computer Science: A Structured Programming Approach Using C 50
FIGURE 12-19 Passing Structures Through Pointers
Page 51
Computer Science: A Structured Programming Approach Using C 51
PROGRAM 12-6 Passing Structures through Pointers
Page 52
Computer Science: A Structured Programming Approach Using C 52
PROGRAM 12-6 Passing Structures through Pointers
Page 53
Computer Science: A Structured Programming Approach Using C 53
PROGRAM 12-6 Passing Structures through Pointers
Page 54
Computer Science: A Structured Programming Approach Using C 54
PROGRAM 12-6 Passing Structures through Pointers
Page 55
Computer Science: A Structured Programming Approach Using C 55
12-4 Unions
The union is a construct that allows memory to be
shared by different types of data. This redefinition can
be as simple as redeclaring an integer as four
characters or as complex as redeclaring an entire
structure.
Referencing Unions
Initializers
Unions and Structures
Internet Addresses
Topics discussed in this section:
Page 56
Computer Science: A Structured Programming Approach Using C 56
FIGURE 12-20 Unions
Page 57
Computer Science: A Structured Programming Approach Using C 57
PROGRAM 12-7 Demonstrate Effect of Union
Page 58
Computer Science: A Structured Programming Approach Using C 58
PROGRAM 12-7 Demonstrate Effect of Union
Page 59
Computer Science: A Structured Programming Approach Using C 59
FIGURE 12-21 A Name Union
Page 60
Computer Science: A Structured Programming Approach Using C 60
PROGRAM 12-8 Demonstrate Unions in Structures
Page 61
Computer Science: A Structured Programming Approach Using C 61
PROGRAM 12-8 Demonstrate Unions in Structures
Page 62
Computer Science: A Structured Programming Approach Using C 62
PROGRAM 12-8 Demonstrate Unions in Structures
Page 63
Computer Science: A Structured Programming Approach Using C 63
PROGRAM 12-9 Convert IP Address to long
Page 64
Computer Science: A Structured Programming Approach Using C 64
PROGRAM 12-9 Convert IP Address to long
Page 65
Computer Science: A Structured Programming Approach Using C 65
PROGRAM 12-9 Convert IP Address to long
Page 66
Computer Science: A Structured Programming Approach Using C 66
12-5 Programming Application
In this section, we develop a program that simulates
the operation of an elevator. The elevator serves floors
from zero (the basement) to the top floor. The elevator
is very old and is not fully automatic. When people
enter the elevator, they enter their desired floor
number.
Page 67
Computer Science: A Structured Programming Approach Using C 67
FIGURE 12-22 Elevator Structure
Page 68
Computer Science: A Structured Programming Approach Using C 68
FIGURE 12-23 Elevator Structure Chart
Page 69
Computer Science: A Structured Programming Approach Using C 69
FIGURE 12-24 Elevator State Diagram
Page 70
Computer Science: A Structured Programming Approach Using C 70
PROGRAM 12-10 Elevator: main
Page 71
Computer Science: A Structured Programming Approach Using C 71
PROGRAM 12-10 Elevator: main
Page 72
Computer Science: A Structured Programming Approach Using C 72
PROGRAM 12-11 Elevator: Initialize
Page 73
Computer Science: A Structured Programming Approach Using C 73
PROGRAM 12-12 Elevator: Run Elevator
Page 74
Computer Science: A Structured Programming Approach Using C 74
PROGRAM 12-12 Elevator: Run Elevator
Page 75
Computer Science: A Structured Programming Approach Using C 75
PROGRAM 12-12 Elevator: Run Elevator
Page 76
Computer Science: A Structured Programming Approach Using C 76
PROGRAM 12-13 Elevator: Move
Page 77
Computer Science: A Structured Programming Approach Using C 77
PROGRAM 12-13 Elevator: Move
Page 78
Computer Science: A Structured Programming Approach Using C 78
PROGRAM 12-13 Elevator: Move
Page 79
Computer Science: A Structured Programming Approach Using C 79
PROGRAM 12-14 Elevator: Move Up and Move Down
Page 80
Computer Science: A Structured Programming Approach Using C 80
PROGRAM 12-14 Elevator: Move Up and Move Down
Page 81
Computer Science: A Structured Programming Approach Using C 81
PROGRAM 12-14 Elevator: Move Up and Move Down
Page 82
Computer Science: A Structured Programming Approach Using C 82
PROGRAM 12-14 Elevator: Move Up and Move Down
Page 83
Computer Science: A Structured Programming Approach Using C 83
PROGRAM 12-15 Elevator: Any Up and Any Down Request
Page 84
Computer Science: A Structured Programming Approach Using C 84
PROGRAM 12-15 Elevator: Any Up and Any Down Request
Page 85
Computer Science: A Structured Programming Approach Using C 85
PROGRAM 12-16 Elevator: Time Pass
Page 86
Computer Science: A Structured Programming Approach Using C 86
PROGRAM 12-17 Elevator: Terminate
Page 87
Computer Science: A Structured Programming Approach Using C 87
12-6 Software Engineering
In this section we discuss two important aspects of
program design: function coupling and data hiding.
Coupling
Data Hiding
Topics discussed in this section:
Page 88
Computer Science: A Structured Programming Approach Using C 88
Well-structured functions are highly cohesive
and loosely coupled.
Note
Page 89
Computer Science: A Structured Programming Approach Using C 89
Stamp coupling should pass only the data needed.
Note
Page 90
Computer Science: A Structured Programming Approach Using C 90
Don’t bundle unrelated data to reduce the
number of parameters.
Note
Page 91
Computer Science: A Structured Programming Approach Using C 91
Control coupling should be used only to pass status.
Note
Page 92
Computer Science: A Structured Programming Approach Using C 92
Avoid global coupling within a program.
Note
Page 93
Computer Science: A Structured Programming Approach Using C 93
Never use content coupling.
Note
Page 94
Computer Science: A Structured Programming Approach Using C 94
Programming Standard:
Do not place any variables in the global area of a program.
Note