Download presentation
Presentation is loading. Please wait.
1
Chapter 10 C Structures and Unions
C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.
2
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. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-2
3
Declaring Structure Types (1/3)
Syntax of the structure type: struct struct_type { type1 id1; type2 id2; … }; E.g., struct student_info { char[20] name; int age; }; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-3
4
Declaring Structure Types (2/3)
Syntax of the structure type: typedef struct{ type1 id1; type2 id2; … } struct_type; E.g., typedef struct{ char[20] name; int age; } student_info; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-4
5
Declaring Structure Types (3/3)
Declaration: student_info student1,student2; Or (without typedef): struct student_info student1,student2; A hierarchical structure is a structure containing components which are also structures. typedef struct{ int NumOfStudents; student_info students[20]; } class_info; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-5
6
Struct example (without typedef)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-6
7
Struct example (with typedef)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-7
8
Manipulating Structure Types (1/2)
We can reference a component of a structure by the direct component selection operator, which is a period. E.g., strcpy(student1.name, “Chao”); student1.age = 18; printf(“%s is in age %d\n”, student1.name, student1.age); Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-8
9
Component selection operator
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-9
10
Manipulating Structure Types (2/2)
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. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-10
11
Operator precedence in struct type
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-11
12
Manipulating Structure Types (2/2)
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. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-12
13
Struct copy Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-13
14
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; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-14
15
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
16
Copyright ©2004 Pearson Addison-Wesley. All rights reserved.
11-16
17
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-17
18
timeupdate example Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-18
19
timeupdate example Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-19
20
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-20
21
Nested Structures (1/2) Sample Struct Memory placement
22
Nested Structures (2/2) Accessing Nested Struct members
23
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 = ; stulist[3].gpa = 3.0; Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-23
24
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-24
25
Struct Array Example (1/2)
26
Struct Array Example (2/2)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.