20070103 chap11 Chapter 11 Structure and Union Types.

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

Programming in C Chapter 10 Structures and Unions
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
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.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
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.
1 C++ Syntax and Semantics The Development Process.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
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.
CS 201 Structure & Union Debzani Deb.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
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;
Structure and Union Types 程式設計 潘仁義 CCU COMM. Structure Type Definition struct structured data objects, can be defined by users #define STRSIZ 10 typedef.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
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.
Functions, Pointers, Structures Keerthi Nelaturu.
Chapter 8 Arrays and Strings
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
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:
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
By Mr. Muhammad Pervez Akhtar
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
© 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.
Pointers What is the data type of pointer variables?
CS1010 Programming Methodology
Simple Data Types and Function Calls
Structure and Union Types
Structure and Union Types
READING AND PRINTING MATRICES (with functions)
Chapter 6 Modular Programming chap6.
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Presentation transcript:

chap11 Chapter 11 Structure and Union Types

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.

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.

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;

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;

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

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.

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.

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;

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.

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

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.

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.

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

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

chap11 16 Functions Whose Result are Structured

chap11 17 Example: Compute an Updated Time Value

chap11 18 Example: Compute an Updated Time Value

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

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

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 = ; stulist[3].gpa = 3.0;

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

chap11 23 Case Study: Universal Measurement Conversion (Fig )

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.

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.

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.