16
Structure and Union Types Chapter 10 1

Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

  • Upload
    dohuong

  • View
    219

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

Structure and Union TypesChapter 10

1

Page 2: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

Chapter Objectives

• To learn how to declare a struct data type which consists of several data fields, each with its own name and data type

• To understand how to use a struct to store data for a structured object or record

• To learn how to use dot notation to process individual fields of a structured object

• To learn how to use structs as function parameters and to return function results

2

Page 3: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

Chapter Objectives

• To see how to create a struct data type for representing complex numbers and how to write functions that perform arithmetic operations on complex numbers

• To understand the relationship between parallel arrays and arrays of structured objects

• To learn about union data types and how they differ form structs

3

Page 4: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

User-Defined Structure Types

• record– a collection of information about one data object

• structure type– a data type for a record composed of multiple

components

• hierarchical structure– a structure containing components that are

structures

4

Page 5: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

User-Defined Structure TypesName: JupiterDiameter: 142,800 kmMoons: 16Orbit time: 11.9 yearsRotation time: 9.925 hours

5

Page 6: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

Individual Components of aStructured Data Object

• direct component selection operator– a period placed between a structure type variable

and a component name to create a reference to the component

6

Page 7: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

7

Page 8: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

8

Page 9: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

Structure Data Type as Input and Output Parameters

• When a structured variable is passed as an input argument to a function, all of its component values are copied into the components of the function’s corresponding formal parameter.

9

Page 10: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

Structure Data Type as Input and Output Parameters

• When such a variable is used as an output argument, the address-of operator must be applied in the same way that we would pass output arguments of the standard types char, int, and double.

10

Page 11: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

11

Page 12: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

12

Page 13: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

Structure Data Type as Input and Output Parameters

• indirect component selection operator– the character sequence -> placed between a

pointer variable and a component name creates a reference that follows the pointer to a structure and selects the component

13

Page 14: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

14

Page 15: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

15

Page 16: Structure and Union Types - Temple Universitytuf80213/courses/temple/cis1057/slides/... · User-Defined Structure Types • record – a collection of information about one data object

16