Structure and Union Types

Slides:



Advertisements
Similar presentations
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
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.
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Programming in C Chapter 10 Structures and Unions
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.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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)
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.
Functions, Pointers, Structures Keerthi Nelaturu.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
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:
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
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.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
chap11 Chapter 11 Structure and Union Types.
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
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.
© 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 and Dynamic Arrays
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
14th September IIT Kanpur
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 14 Graphs and Paths.
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)
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Systems Programming Concepts
Chapter 10 Datapath Subsystems.
Chapter 20 Hash Tables.
(1-1) C Review: Pointers, Arrays, Strings, & Structs
The Facts to Be Explained
Chapter 9: Pointers and String
A simple function.
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)
Circuit Characterization and Performance Estimation
C Pointers Systems Programming.
Chapter 6 Arrays.
Chapter 10 C Structures and Unions
Chapter 9: Records (structs)
Chapter 2 Reference Types.
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Computer Science II CS132/601* Lecture #C-3.
Dynamic Data Structures
Presentation transcript:

Structure and Union Types Chapter 11 Structure and Union Types Instructor: Yuksel / Demirer

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.

Declaring Structure Types (1/2) 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.

Declaring Structure Types (2/2) 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; Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

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.

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

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.

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.

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.

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.

Step-by-Step Analysis of the Indirect Reference “&(*plnp).diameter” is evaluated as shown in the following table. Reference Type Value plnp planet_t * Address of structure refers to current_planet *plnp planet_t Real structure of current_planet (*plnp).diameter double 12713.5 &(*plnp).diameter double * Address of diameter of current_planet structure Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

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

Function Returning a Structured Result Type (1/2) The structure variable can also be used as the return value of a function. Use direct component selection operator. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

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.

Arrays of Structures (1/2) We can also declare an array of structures. E.g., typedef strucut{ 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.

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.

Union Types Union is a data structure which represents only one of the component in the declaration. E.g., typedef union{ int wears_wig; char color[20]; } hair_t; Suppose we declare a vairable “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.

A Function Using a Union Structure 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. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.

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