Chapter 11 Structure and Union Types UserDefined Structure

  • Slides: 40
Download presentation
Chapter 11 Structure and Union Types

Chapter 11 Structure and Union Types

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

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) type for each category of a structured data object. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -2

Declaring Structure Types (1/3) • Syntax of the structure type: struct_type { type 1

Declaring Structure Types (1/3) • Syntax of the structure type: struct_type { type 1 id 1; type 2 id 2; … }; • E. g. , struct student_info { char[20] name; int age; }; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -3

Declaring Structure Types (2/3) • Syntax of the structure type: typedef struct{ struct type

Declaring Structure Types (2/3) • Syntax of the structure type: typedef struct{ struct type 1 id 1; type 2 id 2; … } struct_type; struct_type • E. g. , typedef struct{ struct char[20] name; int age; } student_info; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -4

Declaring Structure Types (3/3) • Declaration: student_info student 1, student 2; • Or (without

Declaring Structure Types (3/3) • Declaration: student_info student 1, student 2; • Or (without typedef): struct student_info student 1, student 2; • A hierarchical structure is a structure containing components which are also structures. typedef struct{ struct int Num. Of. Students; student_info students[20]; } class_info; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -5

Struct example (without typedef) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -6

Struct example (without typedef) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -6

Struct example (with typedef) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -7

Struct example (with typedef) Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -7

Manipulating Structure Types (1/2) • We can reference a component of a structure by

Manipulating Structure Types (1/2) • 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, “Chao”); student 1. age = 18; printf(“%s is in age %dn”, student 1. name, student 1. age); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -8

Component selection operator Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -9

Component selection operator Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -9

Manipulating Structure Types (2/2) • The direct component selection operator has the highest priority

Manipulating Structure Types (2/2) • The direct component selection operator has the highest priority in the operator precedence. – student 1. age+student 2. age+…; – The value of student 1. age is referenced first. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -10

Operator precedence in struct type Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11

Operator precedence in struct type Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -11

Manipulating Structure Types (2/2) • The copy of an entire structure can be easily

Manipulating Structure Types (2/2) • The copy of an entire structure can be easily done by the assignment operator. – student 1 = student 2; – Each component in one structure is copied into the corresponding component in the other structure. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -12

Struct copy Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -13

Struct copy Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -13

Function with a Structured Input Parameter (1/2) • Suppose there is a structure defined

Function with a Structured Input Parameter (1/2) • Suppose there is a structure defined as follows. • typedef struct{ char name[20]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; planet_t Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -14

Function with a Structured Input Parameter (2/2) • When a structure variable is passed

Function with a Structured Input Parameter (2/2) • When a structure variable is passed as an input argument to a function, all its component values are copied into the local structure variable. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -15

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -16

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -16

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

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. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -17

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -18

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -18

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

Data Areas of call to scan_planet (&current_planet); plnp is a pointer which points to the same data area of current_planet. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -19

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

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: -> operator ->. – e. g. , “&plnp->diameter” is the same as “&(*plnp). diameter”. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -20

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -21

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -21

Function Returning a Structured Result Type (1/2) • The structure variable can also be

Function Returning a Structured Result Type (1/2) • The structure variable can also be used as the return value of a function. Return time_of_day struct Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -22

timeupdate example Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -23

timeupdate example Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -23

timeupdate example Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -24

timeupdate example Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -24

Function Returning a Structured Result Type (2/2) • Suppose the current time is 21:

Function Returning a Structured Result Type (2/2) • Suppose the current time is 21: 58: 32, and the elapsed time is 97 seconds. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -25

Nested Structures (1/2) Sample Struct Memory placement

Nested Structures (1/2) Sample Struct Memory placement

Nested Structures (2/2) Accessing Nested Struct members

Nested Structures (2/2) Accessing Nested Struct members

Arrays of Structures (1/2) • We can also declare an array of structures. •

Arrays of Structures (1/2) • We can also declare an array of structures. • E. g. , typedef struct{ int id; double gpa; } student_t; • Usage: student_t stulist[50]; stulist[3]. id = 92922023; stulist[3]. gpa = 3. 0; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -28

Arrays of Structures (2/2) • The array of structures can be simply manipulated as

Arrays of Structures (2/2) • The array of structures can be simply manipulated as arrays of simple data types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -29

Struct Array Example (1/2)

Struct Array Example (1/2)

Struct Array Example (2/2)

Struct Array Example (2/2)

Union Types • Unions are C variables whose syntax look similar to structures, but

Union Types • Unions are C variables whose syntax look similar to structures, but act in a completely different manner. A union is a variable that can take on different data types in different situations. The union syntax is: union tag_name { type 1 member 1 ; type 2 member 2 ; . . . }; • Once a union variable has been declared, the amount of memory reserved is just enough to be able to represent the largest member. (Unlike a structure where memory is reserved for all members). Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -32

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

Union Types • Union is a data structure which represents only one of the component in the declaration. • E. g. , typedef union{ union int wears_wig; char color[20]; } hair_t; hair_t • Suppose we declare a variable “hair_t hair_data; ”. • This hair_data contains either the wears_wig component or the color component but not both. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 11 -33

Union Example

Union Example

Union Types

Union Types

Union Example

Union Example

Difference between union and structure in terms of Memory • Though unions are similar

Difference between union and structure in terms of Memory • Though unions are similar to structure in so many ways, the difference between them is crucial to understand. • As you know, all members of structure can be accessed at any time. But, only one member of union can be accessed at a time in case of union and other members will contain garbage value. • This can be demonstrated by this example:

Difference between union and structure

Difference between union and structure

Difference between union and structure • Output • There is difference in memory allocation

Difference between union and structure • Output • There is difference in memory allocation between union and structure as suggested in above example. The amount of memory required to store a structure variables is the sum of memory size of all members.

Difference between union and structure • But, the memory required to store a union

Difference between union and structure • But, the memory required to store a union variable is the memory required for largest element of an union.