Chapter 10 C Structures and Unions

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Chapter 11 Structure and Union Types Instructor: Alkar / Demirer.
CS 201 Structure & Union Debzani Deb.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
chap11 Chapter 11 Structure and Union Types.
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10.
Introduction We will study how to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining.
Structures and Classes
Implementing Subprograms
© 2016 Pearson Education, Ltd. All rights reserved.
CS1010 Programming Methodology
Chapter 10-1: Structure.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Chapter 14 Graphs and Paths.
Chapter 12 Collections.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Simple Data Types and Function Calls
Structure and Union Types
Chapter 9: Records (structs)
Chapter 4 (part 2).
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Systems Programming Concepts
Arrays and Arrays as Parameters
Chapter 10 Datapath Subsystems.
Chapter 20 Hash Tables.
Copyright © 2011 Pearson Education, Inc
Searching for Guinea Pig B: Case Study in Online Research
Structure and Union Types
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Chapter 5 Algorithm Analysis.
Chapter 6 Data Types.
Chapter 12 Collections.
EECE.2160 ECE Application Programming
The Facts to Be Explained
Introduction: Some Representative Problems
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Structure (i.e. struct) An structure creates a user defined data type
READING AND PRINTING MATRICES (with functions)
Multidimensional Arrays Section 6.4
Circuit Characterization and Performance Estimation
User-defined data types
EECE.2160 ECE Application Programming
Chapter 2 Part 1 Data and Expressions.
Chapter 6 Dynamic Programming.
Chapter 9: Records (structs)
Chapter 2 Reference Types.
Chapter 11 Structure and Union Types.
Chapter 4 Greedy Algorithms.
Copyright © 2011 Pearson Education, Inc
Structures, Unions, and Enumerations
Dynamic Data Structures
Presentation transcript:

Chapter 10 C Structures and Unions C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.

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

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

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

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

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

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

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 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

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 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

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 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

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

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

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

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

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

Nested Structures (1/2) Sample Struct Memory placement

Nested Structures (2/2) Accessing Nested Struct members

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-23

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

Struct Array Example (1/2)

Struct Array Example (2/2)