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
Computer Science: A Structured Programming Approach Using C 2
FIGURE 12-1 Derived Types
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.
Computer Science: A Structured Programming Approach Using C 4
FIGURE 12-2 Type-definition Format
typedef int INGETER;
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:
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
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
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
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
Anonymous Enumeration: Constants enum {space = ‘ ‘, comma = ‘,’,
color = ‘:’};
enum {OFF, ON};
Computer Science: A Structured Programming Approach Using C 10
Computer Science: A Structured Programming Approach Using C 11
PROGRAM 12-1 Print Cable TV Stations
Computer Science: A Structured Programming Approach Using C 12
PROGRAM 12-1 Print Cable TV Stations
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
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:
Computer Science: A Structured Programming Approach Using C 15
FIGURE 12-3 Structure Examples
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
Computer Science: A Structured Programming Approach Using C 17
FIGURE 12-4 Tagged Structure Format
Computer Science: A Structured Programming Approach Using C 18
FIGURE 12-5 Structure Declaration with typedef
Computer Science: A Structured Programming Approach Using C 19
FIGURE 12-6 Structure Declaration Format and Example
Computer Science: A Structured Programming Approach Using C 20
FIGURE 12-7 Initializing Structures
Computer Science: A Structured Programming Approach Using C 21
FIGURE 12-8 Structure Direct Selection Operator
Computer Science: A Structured Programming Approach Using C 22
PROGRAM 12-2 Multiply Fractions
Computer Science: A Structured Programming Approach Using C 23
PROGRAM 12-2 Multiply Fractions
Computer Science: A Structured Programming Approach Using C 24
FIGURE 12-9 Copying a Structure
Computer Science: A Structured Programming Approach Using C 25
FIGURE 12-10 Pointers to Structures
Computer Science: A Structured Programming Approach Using C 26
(*pointerName).fieldName pointerName->fieldName.
Note
Computer Science: A Structured Programming Approach Using C 27
FIGURE 12-11 Interpretation of Invalid Pointer Use
Computer Science: A Structured Programming Approach Using C 28
FIGURE 12-12 Indirect Selection Operator
Computer Science: A Structured Programming Approach Using C 29
PROGRAM 12-3 Clock Simulation with Pointers
Computer Science: A Structured Programming Approach Using C 30
PROGRAM 12-3 Clock Simulation with Pointers
Computer Science: A Structured Programming Approach Using C 31
PROGRAM 12-3 Clock Simulation with Pointers
Computer Science: A Structured Programming Approach Using C 32
PROGRAM 12-3 Clock Simulation with Pointers
Computer Science: A Structured Programming Approach Using C 33
FIGURE 12-13 Nested Structure
Computer Science: A Structured Programming Approach Using C 34
FIGURE 12-14 Arrays in Structures
Computer Science: A Structured Programming Approach Using C 35
FIGURE 12-15 Pointers in Structures
Computer Science: A Structured Programming Approach Using C 36
FIGURE 12-16 Array of Structures
Computer Science: A Structured Programming Approach Using C 37
PROGRAM 12-4 Sort Array of Student Structures
Computer Science: A Structured Programming Approach Using C 38
PROGRAM 12-4 Sort Array of Student Structures
Computer Science: A Structured Programming Approach Using C 39
PROGRAM 12-4 Sort Array of Student Structures
Computer Science: A Structured Programming Approach Using C 40
PROGRAM 12-4 Sort Array of Student Structures
Computer Science: A Structured Programming Approach Using C 41
PROGRAM 12-4 Sort Array of Student Structures
Computer Science: A Structured Programming Approach Using C 42
PROGRAM 12-4 Sort Array of Student Structures
Computer Science: A Structured Programming Approach Using C 43
FIGURE 12-17 Passing Structure Members to Functions
Computer Science: A Structured Programming Approach Using C 44
FIGURE 12-18 Passing and returning structures
Computer Science: A Structured Programming Approach Using C 45
PROGRAM 12-5 Passing and Returning Structures
Computer Science: A Structured Programming Approach Using C 46
PROGRAM 12-5 Passing and Returning Structures
Computer Science: A Structured Programming Approach Using C 47
PROGRAM 12-5 Passing and Returning Structures
Computer Science: A Structured Programming Approach Using C 48
PROGRAM 12-5 Passing and Returning Structures
Computer Science: A Structured Programming Approach Using C 49
PROGRAM 12-5 Passing and Returning Structures
Computer Science: A Structured Programming Approach Using C 50
FIGURE 12-19 Passing Structures Through Pointers
Computer Science: A Structured Programming Approach Using C 51
PROGRAM 12-6 Passing Structures through Pointers
Computer Science: A Structured Programming Approach Using C 52
PROGRAM 12-6 Passing Structures through Pointers
Computer Science: A Structured Programming Approach Using C 53
PROGRAM 12-6 Passing Structures through Pointers
Computer Science: A Structured Programming Approach Using C 54
PROGRAM 12-6 Passing Structures through Pointers
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:
Computer Science: A Structured Programming Approach Using C 56
FIGURE 12-20 Unions
Computer Science: A Structured Programming Approach Using C 57
PROGRAM 12-7 Demonstrate Effect of Union
Computer Science: A Structured Programming Approach Using C 58
PROGRAM 12-7 Demonstrate Effect of Union
Computer Science: A Structured Programming Approach Using C 59
FIGURE 12-21 A Name Union
Computer Science: A Structured Programming Approach Using C 60
PROGRAM 12-8 Demonstrate Unions in Structures
Computer Science: A Structured Programming Approach Using C 61
PROGRAM 12-8 Demonstrate Unions in Structures
Computer Science: A Structured Programming Approach Using C 62
PROGRAM 12-8 Demonstrate Unions in Structures
Computer Science: A Structured Programming Approach Using C 63
PROGRAM 12-9 Convert IP Address to long
Computer Science: A Structured Programming Approach Using C 64
PROGRAM 12-9 Convert IP Address to long
Computer Science: A Structured Programming Approach Using C 65
PROGRAM 12-9 Convert IP Address to long
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.
Computer Science: A Structured Programming Approach Using C 67
FIGURE 12-22 Elevator Structure
Computer Science: A Structured Programming Approach Using C 68
FIGURE 12-23 Elevator Structure Chart
Computer Science: A Structured Programming Approach Using C 69
FIGURE 12-24 Elevator State Diagram
Computer Science: A Structured Programming Approach Using C 70
PROGRAM 12-10 Elevator: main
Computer Science: A Structured Programming Approach Using C 71
PROGRAM 12-10 Elevator: main
Computer Science: A Structured Programming Approach Using C 72
PROGRAM 12-11 Elevator: Initialize
Computer Science: A Structured Programming Approach Using C 73
PROGRAM 12-12 Elevator: Run Elevator
Computer Science: A Structured Programming Approach Using C 74
PROGRAM 12-12 Elevator: Run Elevator
Computer Science: A Structured Programming Approach Using C 75
PROGRAM 12-12 Elevator: Run Elevator
Computer Science: A Structured Programming Approach Using C 76
PROGRAM 12-13 Elevator: Move
Computer Science: A Structured Programming Approach Using C 77
PROGRAM 12-13 Elevator: Move
Computer Science: A Structured Programming Approach Using C 78
PROGRAM 12-13 Elevator: Move
Computer Science: A Structured Programming Approach Using C 79
PROGRAM 12-14 Elevator: Move Up and Move Down
Computer Science: A Structured Programming Approach Using C 80
PROGRAM 12-14 Elevator: Move Up and Move Down
Computer Science: A Structured Programming Approach Using C 81
PROGRAM 12-14 Elevator: Move Up and Move Down
Computer Science: A Structured Programming Approach Using C 82
PROGRAM 12-14 Elevator: Move Up and Move Down
Computer Science: A Structured Programming Approach Using C 83
PROGRAM 12-15 Elevator: Any Up and Any Down Request
Computer Science: A Structured Programming Approach Using C 84
PROGRAM 12-15 Elevator: Any Up and Any Down Request
Computer Science: A Structured Programming Approach Using C 85
PROGRAM 12-16 Elevator: Time Pass
Computer Science: A Structured Programming Approach Using C 86
PROGRAM 12-17 Elevator: Terminate
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:
Computer Science: A Structured Programming Approach Using C 88
Well-structured functions are highly cohesive
and loosely coupled.
Note
Computer Science: A Structured Programming Approach Using C 89
Stamp coupling should pass only the data needed.
Note
Computer Science: A Structured Programming Approach Using C 90
Don’t bundle unrelated data to reduce the
number of parameters.
Note
Computer Science: A Structured Programming Approach Using C 91
Control coupling should be used only to pass status.
Note
Computer Science: A Structured Programming Approach Using C 92
Avoid global coupling within a program.
Note
Computer Science: A Structured Programming Approach Using C 93
Never use content coupling.
Note
Computer Science: A Structured Programming Approach Using C 94
Programming Standard:
Do not place any variables in the global area of a program.
Note
Top Related