Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 10.
Chapter 11 Structure and Union Types Instructor: Alkar / Demirer.
Topic 11 – Structures and Unions. CISC105 – Topic 11 Introduction to Structures We have seen that an array is a list of the same type of elements. A structure.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
CS 201 Structure & Union Debzani Deb.
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
CS 201 Functions Debzani Deb.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
Structure and Union Types 程式設計 潘仁義 CCU COMM. Structure Type Definition struct structured data objects, can be defined by users #define STRSIZ 10 typedef.
TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 8 Arrays and Strings
Introduction to C Programming
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Chapter 15: Operator Overloading
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
chap11 Chapter 11 Structure and Union Types.
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
© 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.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
C Structures, Unions, Bit Manipulations and Enumerations
Structure and Union Types
Lecture 18 Arrays and Pointer Arithmetic
Structure and Union Types
READING AND PRINTING MATRICES (with functions)
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Presentation transcript:

Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-2 User-Defined Structure Types record –a collection of information about one data object A database is a collection of information stored in a computer’s memory or in a disk file. –a database is subdivided into records

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-3 Structure Type Definition Example –Name: Jupiter –Diameter: 142,800 km –Moons: 16 –Orbit time: 11.9 yr –Rotation time: hr structure type –a data type for a record composed of multiple components

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-4 Structure Type Definition (Cont’d) #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; /* equatorial diameter in km */ int moons; /* number of moons */ double orbit_time, /* years to orbit sun once */ rotation_time; /* hours to complete one revolution on axis */ } planet_t;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-5 Structure Type Definition (Cont’d) A name chosen for a component of one structure may be the same as the name of a component of another structure or the same as the name of a variable. The typedef statement itself allocates no memory A variable declaration is required to allocate storage space for a structured data object –planet_t current_planet, previous_planet, blank_planet = {"", 0, 0, 0, 0};

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-6 Structure Type Definition (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-7 Structure Type Definition (Cont’d) hierarchical structure –a structure containing components that are structures Example typedef struct { double diameter; planet_t planets[9]; char galaxy[STRSIZ]; } solar_sys_t;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-8 Structure Type Definition (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-9 Manipulating Individual Components of a Structured Data Object direct component selection operator –a period placed between a structure type variable and a component name to create a reference to the component

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.1 Assigning Values to Components of Variable current_planet

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Manipulating Individual Components of a Structured Data Object (Cont’d) printf("%s's equatorial diameter is %.1f km.\n", current_planet.name, current_planet.diameter); –Jupiter's equatorial diameter is km.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Review of Operator Precedence In a generic expression containing two of the same operators in sequence: –operand1 op operand2 op operand3

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Review of Operator Precedence (Cont’d) Left associativity –(operand1 op operand2) op operand3 Right associativity –operand1 op (operand2 op operand3)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Manipulating Whole Structures With no component selection operator refers to the entire structure –previous_planet = current_planet;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Program Style Naming Convention for Types To help reduce confusion, choose user- defined type names ending in the suffix _t.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Structure Type Data as Input and Output Parameters When a structured variable is passed as an input argument to a function, all of its component values are copied into the components of the function’s corresponding formal parameter. When such a variable is used as an output argument, the address-of operator must be applied. The equality and inequality operators cannot be applied to a structured type as a unit.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.2 Function with a Structured Input Parameter print_planet(current_planet);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.3 Function Comparing Two Structured Values for Equality

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.4 Function with a Structured Output Argument

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Structure Type Data as Input and Output Parameters (Cont’d) In order to use scanf to store a value in one component of the structure whose address is in plnp, we must carry out the following steps (in order): 1.Follow the pointer in plnp to the structure. 2.Select the component of interest. 3.Unless this component is an array (e.g., component name in Fig. 11.4), get its address to pass to scanf. &*plnp.diameter would attempt step 2 before step 1.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Structure Type Data as Input and Output Parameters (Cont’d) indirect component selection operator –the character sequence -> placed between a pointer variable and a component name creates a reference that follows the pointer to a structure and selects the component two expressions are equivalent. –(*structp).component –structp->component

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Structure Type Data as Input and Output Parameters (Cont’d) result = scanf("%s%lf%d%lf%lf", plnp->name, &plnp->diameter, &plnp->moons, &plnp->orbit_time, &plnp->rotation_time);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.5 Data Areas of main and scan_planet during Execution of status = scan_planet (&current_planet);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Structure Type Data as Input and Output Parameters (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Functions Whose Result Values Are Structured The function does not return the address of the structure as it would with an array result; rather it returns the values of all components.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.6 Function get_planet Returning a Structured Result Type current_planet = get_planet(); However, scan_planet with its ability to return an integer error code is the more generally useful function.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.7 Function to Compute an Updated Time Value typedef struct { int hour, minute, second; } time_t; time_now = new_time(time_now, secs);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.8 Structured Values as a Function Input Argument and as a Function Result

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Abstract Data Type abstract data type (ADT) –a data type combined with a set of basic operations We must also provide basic operations for manipulating our own data types. If we take the time to define enough basic operations for a structure type, we then find it possible to think about a related problem at a higher level of abstraction.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure 11.9 Data Type planet_t and Basic Operations

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parallel Arrays and Arrays of Structures Parallel Arrays int id[50]; /* id numbers and */ double gpa[50]; /* gpa's of up to 50 students */ double x[NUM_PTS], /* (x,y) coordinates of */, y[NUM_PTS]; /* up to NUM_PTS points */ Array of Structures –A more natural and convenient organization is to group the information in a structure whose type we define.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parallel Arrays and Arrays of Structures (Cont’d) Example #define MAX_STU 50 typedef struct { int id; double gpa; } student_t;... { student_t stulist[MAX_STU]; Example #define NUM_PTS 10 typedef struct { double x, y; } point_t;... { point_t polygon[NUM_PTS];

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Parallel Arrays and Arrays of Structures (Cont’d) stulist[0].id stulist[0].gpa for (i = 0; i < MAX_STU; ++i) scan_student(&stulist[i]); for (i = 0; i < MAX_STU; ++i) printf("%d\n", stulist[i].id);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure An Array of Structures

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Union Types union –a data structure that overlays components in memory, allowing one chunk of memory to be interpreted in multiple ways typedef union { int wears_wig; char color[20]; } hair_t; hair_t hair_data; hair_data does not contain both wears_wig and color components, but either a wears_wig component referenced by hair_data.wears_wig, or a color component referenced by hair_data.color. The amount of memory is determined by the largest component of the union.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Union Types (Cont’d) typedef struct { int bald; hair_t h; } hair_info_t; base our manipulation on the value of the bald component Referencing the appropriate union component is always the programmer’s responsibility; C can do no checking of the validity of such a component reference.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Function That Displays a Structure with a Union Type Component

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Two Interpretations of Parameter hair

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Program to Compute Area and Perimeter of Geometri c Figures

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Program to Compute Area and Perimeter of Geometric Figures (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Program to Compute Area and Perimeter of Geometric Figures (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Program to Compute Area and Perimeter of Geometric Figures (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Common Programming Errors Write your own type-specific equality and I/O functions. Place the union within another structure that contains a component whose value indicates which interpretation of the union is correct.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Partial Implementation of Type and Operators for Complex Numbers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Universal Measurement Conversion Program Using an Array of Structures

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved Figure Data File and Sample Run of Measurement Conversion Program