You are on page 1of 40

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.

Topics discussed in this section:


Referencing Unions
Initializers
Unions and Structures
Internet Addresses
Computer Science: A Structured Programming Approach Using C 1
FIGURE 12-20 Unions

Computer Science: A Structured Programming Approach Using C 2


PROGRAM 12-7 Demonstrate Effect of Union

Computer Science: A Structured Programming Approach Using C 3


PROGRAM 12-7 Demonstrate Effect of Union

Computer Science: A Structured Programming Approach Using C 4


FIGURE 12-21 A Name Union

Computer Science: A Structured Programming Approach Using C 5


PROGRAM 12-8 Demonstrate Unions in Structures

Computer Science: A Structured Programming Approach Using C 6


PROGRAM 12-8 Demonstrate Unions in Structures

Computer Science: A Structured Programming Approach Using C 7


PROGRAM 12-8 Demonstrate Unions in Structures

Computer Science: A Structured Programming Approach Using C 8


PROGRAM 12-9 Convert IP Address to long

Computer Science: A Structured Programming Approach Using C 9


PROGRAM 12-9 Convert IP Address to long

Computer Science: A Structured Programming Approach Using C 10


PROGRAM 12-9 Convert IP Address to long

Computer Science: A Structured Programming Approach Using C 11


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 12


FIGURE 12-22 Elevator Structure

Computer Science: A Structured Programming Approach Using C 13


FIGURE 12-23 Elevator Structure Chart

Computer Science: A Structured Programming Approach Using C 14


FIGURE 12-24 Elevator State Diagram

Computer Science: A Structured Programming Approach Using C 15


PROGRAM 12-10 Elevator: main

Computer Science: A Structured Programming Approach Using C 16


PROGRAM 12-10 Elevator: main

Computer Science: A Structured Programming Approach Using C 17


PROGRAM 12-11 Elevator: Initialize

Computer Science: A Structured Programming Approach Using C 18


PROGRAM 12-12 Elevator: Run Elevator

Computer Science: A Structured Programming Approach Using C 19


PROGRAM 12-12 Elevator: Run Elevator

Computer Science: A Structured Programming Approach Using C 20


PROGRAM 12-12 Elevator: Run Elevator

Computer Science: A Structured Programming Approach Using C 21


PROGRAM 12-13 Elevator: Move

Computer Science: A Structured Programming Approach Using C 22


PROGRAM 12-13 Elevator: Move

Computer Science: A Structured Programming Approach Using C 23


PROGRAM 12-13 Elevator: Move

Computer Science: A Structured Programming Approach Using C 24


PROGRAM 12-14 Elevator: Move Up and Move Down

Computer Science: A Structured Programming Approach Using C 25


PROGRAM 12-14 Elevator: Move Up and Move Down

Computer Science: A Structured Programming Approach Using C 26


PROGRAM 12-14 Elevator: Move Up and Move Down

Computer Science: A Structured Programming Approach Using C 27


PROGRAM 12-14 Elevator: Move Up and Move Down

Computer Science: A Structured Programming Approach Using C 28


PROGRAM 12-15 Elevator: Any Up and Any Down Request

Computer Science: A Structured Programming Approach Using C 29


PROGRAM 12-15 Elevator: Any Up and Any Down Request

Computer Science: A Structured Programming Approach Using C 30


PROGRAM 12-16 Elevator: Time Pass

Computer Science: A Structured Programming Approach Using C 31


PROGRAM 12-17 Elevator: Terminate

Computer Science: A Structured Programming Approach Using C 32


12-6 Software Engineering

In this section we discuss two important aspects of


program design: function coupling and data hiding.

Topics discussed in this section:


Coupling
Data Hiding

Computer Science: A Structured Programming Approach Using C 33


Note
Well-structured functions are highly cohesive
and loosely coupled.

Computer Science: A Structured Programming Approach Using C 34


Note
Stamp coupling should pass only the data needed.

Computer Science: A Structured Programming Approach Using C 35


Note
Don’t bundle unrelated data to reduce the
number of parameters.

Computer Science: A Structured Programming Approach Using C 36


Note
Control coupling should be used only to pass status.

Computer Science: A Structured Programming Approach Using C 37


Note
Avoid global coupling within a program.

Computer Science: A Structured Programming Approach Using C 38


Note
Never use content coupling.

Computer Science: A Structured Programming Approach Using C 39


Note
Programming Standard:
Do not place any variables in the global area of a program.

Computer Science: A Structured Programming Approach Using C 40

You might also like