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