Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Enumerated, Structure, and Union Types Objectives

Similar presentations


Presentation on theme: "Chapter 12 Enumerated, Structure, and Union Types Objectives"— Presentation transcript:

1 Chapter 12 Enumerated, Structure, and Union Types 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. Computer Science: A Structured Programming Approach Using C

2 FIGURE 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
Computer Science: A Structured Programming Approach Using C

5 Topics discussed in this section:
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. Topics discussed in this section: Declaring an Enumerated Type Operations on Enumerated Types Enumeration Type Conversion Initializing Enumerated Constants Anonymous Enumeration: Constants Input/Output Operations Computer Science: A Structured Programming Approach Using C

6 Print Cable TV Stations
PROGRAM 12-1 Print Cable TV Stations Computer Science: A Structured Programming Approach Using C

7 Print Cable TV Stations
PROGRAM 12-1 Print Cable TV Stations Computer Science: A Structured Programming Approach Using C

8 Note 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. Computer Science: A Structured Programming Approach Using C

9 Topics discussed in this section:
Structure A structure is a collection of related elements, possibly of different types, having a single name. Topics discussed in this section: Structure Type Declaration Initialization Accessing Structures Operations on Structures Complex Structures Structures and Functions Computer Science: A Structured Programming Approach Using C

10 FIGURE 12-3 Structure Examples
Computer Science: A Structured Programming Approach Using C

11 should be logically related.
Note Elements in a structure can be of the same or different types. However, all elements in the structure should be logically related. Computer Science: A Structured Programming Approach Using C

12 FIGURE 12-4 Tagged Structure Format
Computer Science: A Structured Programming Approach Using C

13 FIGURE 12-5 Structure Declaration with typedef
Computer Science: A Structured Programming Approach Using C

14 FIGURE 12-6 Structure Declaration Format and Example
Computer Science: A Structured Programming Approach Using C

15 FIGURE 12-7 Initializing Structures
Computer Science: A Structured Programming Approach Using C

16 FIGURE 12-8 Structure Direct Selection Operator
Computer Science: A Structured Programming Approach Using C

17 PROGRAM 12-2 Multiply Fractions
Computer Science: A Structured Programming Approach Using C

18 PROGRAM 12-2 Multiply Fractions
Computer Science: A Structured Programming Approach Using C

19 FIGURE 12-9 Copying a Structure
Computer Science: A Structured Programming Approach Using C

20 FIGURE 12-10 Pointers to Structures
Computer Science: A Structured Programming Approach Using C

21 (*pointerName).fieldName « pointerName->fieldName.
Note (*pointerName).fieldName « pointerName->fieldName. Computer Science: A Structured Programming Approach Using C

22 FIGURE 12-11 Interpretation of Invalid Pointer Use
Computer Science: A Structured Programming Approach Using C

23 FIGURE 12-12 Indirect Selection Operator
Computer Science: A Structured Programming Approach Using C

24 Clock Simulation with Pointers
PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

25 Clock Simulation with Pointers
PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

26 Clock Simulation with Pointers
PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

27 Clock Simulation with Pointers
PROGRAM 12-3 Clock Simulation with Pointers Computer Science: A Structured Programming Approach Using C

28 FIGURE 12-13 Nested Structure
Computer Science: A Structured Programming Approach Using C

29 FIGURE 12-14 Arrays in Structures
Computer Science: A Structured Programming Approach Using C

30 FIGURE 12-15 Pointers in Structures
Computer Science: A Structured Programming Approach Using C

31 FIGURE 12-16 Array of Structures
Computer Science: A Structured Programming Approach Using C

32 Sort Array of Student Structures
PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

33 Sort Array of Student Structures
PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

34 Sort Array of Student Structures
PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

35 Sort Array of Student Structures
PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

36 Sort Array of Student Structures
PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

37 Sort Array of Student Structures
PROGRAM 12-4 Sort Array of Student Structures Computer Science: A Structured Programming Approach Using C

38 FIGURE 12-17 Passing Structure Members to Functions
Computer Science: A Structured Programming Approach Using C

39 FIGURE 12-18 Passing and returning structures
Computer Science: A Structured Programming Approach Using C

40 Passing and Returning Structures
PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

41 Passing and Returning Structures
PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

42 Passing and Returning Structures
PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

43 Passing and Returning Structures
PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

44 Passing and Returning Structures
PROGRAM 12-5 Passing and Returning Structures Computer Science: A Structured Programming Approach Using C

45 FIGURE 12-19 Passing Structures Through Pointers
Computer Science: A Structured Programming Approach Using C

46 Passing Structures through Pointers
PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C

47 Passing Structures through Pointers
PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C

48 Passing Structures through Pointers
PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C

49 Passing Structures through Pointers
PROGRAM 12-6 Passing Structures through Pointers Computer Science: A Structured Programming Approach Using C


Download ppt "Chapter 12 Enumerated, Structure, and Union Types Objectives"

Similar presentations


Ads by Google