Presentation is loading. Please wait.

Presentation is loading. Please wait.

20070103 chap11 Chapter 11 Structure and Union Types.

Similar presentations


Presentation on theme: "20070103 chap11 Chapter 11 Structure and Union Types."— Presentation transcript:

1 20070103 chap11 Chapter 11 Structure and Union Types

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

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

4 20070103 chap11 4 Declaring Structure Types Syntax typedef struct struct_type typedef struct{ type1 id1; type2 id2; … } struct_type; typedef struct student_info; example typedef struct{ char[20] name; int age; } student_info;

5 20070103 chap11 5 Declaring Structure Types Declaration: student_info student1, student2 = {“Wang”, 18}; A hierarchical structure is a structure containing components which are also structures. typedef struct{ int NumOfStudents; student_info students[20]; } class_info;

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

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

8 20070103 chap11 8 Structure Type Data as Input & Output Parameters 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.

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

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

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

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

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

14 20070103 chap11 14 Step-by-Step Analysis of the Indirect Reference

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

16 20070103 chap11 16 Functions Whose Result are Structured

17 20070103 chap11 17 Example: Compute an Updated Time Value

18 20070103 chap11 18 Example: Compute an Updated Time Value

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

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

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

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

23 20070103 chap11 23 Case Study: Universal Measurement Conversion (Fig. 11.12)

24 20070103 chap11 24 Union Types 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; Suppose we declare a vairable “ hait_t hair_data; ”. This hair_data contains either the wears_wig component or the color component but not both.

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

26 20070103 chap11 26 Two Interpretations of the Union Variable hair_t The memory content of hair_t depends on which component is referenced. 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. The color component is referenced.


Download ppt "20070103 chap11 Chapter 11 Structure and Union Types."

Similar presentations


Ads by Google