Chapter 11 Structure and Union Types 20070103 chap

  • Slides: 26
Download presentation
Chapter 11 Structure and Union Types 20070103 chap 11

Chapter 11 Structure and Union Types 20070103 chap 11

Objectives n n We have seen how to represent numbers, characters, words, other strings,

Objectives n n We have seen how to represent numbers, characters, words, other strings, and lists (arrays) of these objects. We will show to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining to particular objects. 20070103 chap 11 2

User-Defined Structure Types n A database is a collection of information subdivided into records.

User-Defined Structure Types n A database is a collection of information subdivided into records. n A record is a collection of information of one data object (e. g. , ID, name, and age of a student). n C allows us to define a new data type (called structure type) for each category of a structured data object. 20070103 chap 11 3

Declaring Structure Types n Syntax typedef struct{ struct type 1 id 1; type 2

Declaring Structure Types n Syntax typedef struct{ struct type 1 id 1; type 2 id 2; … } struct_type; struct_type n example typedef struct{ struct char[20] name; int age; } student_info; 20070103 chap 11 4

Declaring Structure Types n Declaration: student_info student 1, student 2 = {“Wang”, 18}; n

Declaring Structure Types n Declaration: student_info student 1, student 2 = {“Wang”, 18}; n A hierarchical structure is a structure containing components which are also structures. typedef struct{ int Num. Of. Students; student_info students[20]; } class_info; 20070103 chap 11 5

Manipulating Individual Components of a Structured Data Type n n We can reference a

Manipulating Individual Components of a Structured Data Type n n We can reference a component of a structure by the direct component selection operator, operator which is a period. E. g. , strcpy(student 1. name, “Wang”); student 1. age = 18; printf(“%s is in age %dn”, student 1. name, student 1. age); 20070103 chap 11 6

Manipulating Structures n The direct component selection operator has the highest priority in the

Manipulating Structures n The direct component selection operator has the highest priority in the operator precedence. n n n student 1. age+student 2. age+…; The value of student 1. age is referenced first. The copy of an entire structure can be easily done by the assignment operator. n n 20070103 student 1 = student 2; Each component in one structure is copied into the corresponding component in the other structure. chap 11 7

Structure Type Data as Input & Output Parameters n n As an input argument:

Structure Type Data as Input & Output Parameters n n As an input argument: all of its component values are copied into the components of the function’s corresponding formal parameter. As an output argument: the address-of operator must be applied. 20070103 chap 11 8

Function with a Structured Input Parameter n Suppose there is a structure defined as

Function with a Structured Input Parameter n Suppose there is a structure defined as follows. typedef struct{ char name[20]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; 20070103 chap 11 9

Function with a Structured Input Parameter n When a structure variable is passed as

Function with a Structured Input Parameter n When a structure variable is passed as an input argument to a function, all its component values are copied into the local structure variable. 20070103 chap 11 10

Comparing Two Structured Values for Equality n The equality and inequality operators cannot be

Comparing Two Structured Values for Equality n The equality and inequality operators cannot be applied to a structured type as a unit. 20070103 chap 11 11

Function with a Structured Input/Output Argument n For the following function, we have to

Function with a Structured Input/Output Argument n For the following function, we have to call it by “scan_planet(&current_planet); ” n The input argument is also used to store the result. “*plnp” is parenthesized because & operator has higher precedence. 20070103 chap 11 12

Data Areas of call to scan_planet(&current_planet); plnp is a pointer which points to the

Data Areas of call to scan_planet(&current_planet); plnp is a pointer which points to the same data area of current_planet. 20070103 chap 11 13

Step-by-Step Analysis of the Indirect Reference 20070103 chap 11 14

Step-by-Step Analysis of the Indirect Reference 20070103 chap 11 14

Indirect Component Selection Operator n In the above example, we use direct component selection

Indirect Component Selection Operator n In the above example, we use direct component selection operator: period. e. g. , &(*plnp). diameter n C also provides indirect component selection operator: -> &plnp->diameter is the same as &(*plnp). diameter 20070103 chap 11 15

Functions Whose Result are Structured 20070103 chap 11 16

Functions Whose Result are Structured 20070103 chap 11 16

Example: Compute an Updated Time Value 20070103 chap 11 17

Example: Compute an Updated Time Value 20070103 chap 11 17

Example: Compute an Updated Time Value 20070103 chap 11 18

Example: Compute an Updated Time Value 20070103 chap 11 18

Problem Solving with Structure Types n We must also provide basic operations for manipulating

Problem Solving with Structure Types n We must also provide basic operations for manipulating these structure types 20070103 chap 11 19

Case Study: Complex Numbers (Fig. 11. 10) n A complex number is a number

Case Study: Complex Numbers (Fig. 11. 10) n A complex number is a number of a real part and an imaginary part. a+bi 20070103 chap 11 20

Arrays of Structures n We can also declare an array of structures. typedef struct{

Arrays of Structures n We can also declare an array of structures. typedef struct{ int id; double gpa; } student_t; n Usage: student_t stulist[50]; stulist[3]. id = 92922023; stulist[3]. gpa = 3. 0; 20070103 chap 11 21

Arrays of Structures n The array of structures can be simply manipulated as arrays

Arrays of Structures n The array of structures can be simply manipulated as arrays of simple data types. 20070103 chap 11 22

Case Study: Universal Measurement Conversion (Fig. 11. 12) 20070103 chap 11 23

Case Study: Universal Measurement Conversion (Fig. 11. 12) 20070103 chap 11 23

Union Types n Union is a data structure which represents only one of the

Union Types n Union is a data structure which represents only one of the component in the declaration. typedef union{ int wears_wig; char color[20]; } hair_t; n Suppose we declare a vairable n “hait_t hair_data; ”. This hair_data contains either the wears_wig component or the color component but not both. 20070103 chap 11 24

Example n Suppose we have a structure variable. typedef struct{ int bald; hair_t h;

Example n Suppose we have a structure variable. typedef struct{ int bald; hair_t h; } hair_info_t n We can use this structure to reference the correct component. Use the wears_wig component. Use the color component. 20070103 chap 11 25

Two Interpretations of the Union Variable hair_t n The memory content of hair_t depends

Two Interpretations of the Union Variable hair_t n The memory content of hair_t depends on which component is referenced. n n The memory allocated for hair_t is determined by the largest component in the union. Referencing the correct union component is the programmer’s responsibility. The wears_wig component is referenced. 20070103 chap 11 The color component is referenced. 26