Chapter 11 Structure and Union Types Instructor: Alkar / Demirer.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
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.
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.
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. 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.
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
© 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.
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.
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.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Chapter 2 Application Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2.
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)
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;
Chapter 6 Human Capital. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2.
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
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 16 Resources and the Environment at the Global Level.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
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.
Functions, Pointers, Structures Keerthi Nelaturu.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
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,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Lecture 26: Structures / struct s/. 2 Lecture Contents: t Basics of structs t Struct type definition ( struct reserved word) t Struct type definition.
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:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Today’s Material Aggregate Data Types: Structures and Unions
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.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
chap11 Chapter 11 Structure and Union Types.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
© 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.
Simple Data Types and Function Calls
Structure and Union Types
Structure and Union Types
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Computer Science II CS132/601* Lecture #C-3.
Dynamic Data Structures
Presentation transcript:

Chapter 11 Structure and Union Types Instructor: Alkar / Demirer

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-2 User-Defined Structure Types recordsA database is a collection of information subdivided into records. record –A record is a collection of information of one data object (e.g., ID, name, and age of a student). structure typeC 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-3 Declaring Structure Types (1/2) typedef struct struct_typeSyntax of the structure type: typedef struct{ type1 id1; type2 id2; … } struct_type; typedef struct student_info;E.g., typedef struct{ char[20] name; int age; } student_info;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-4 Declaring Structure Types (2/2) student_infoDeclaration: student_info student1, student2 = {“Wang”, 18}; typedef struct student_info class_info;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 Manipulating Structure Types (1/2) direct component selection operatorWe 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-6 Manipulating Structure Types (2/2) direct component selection operatorThe 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.11-7 Function with a Structured Input Parameter (1/2) Suppose there is a structure defined as follows. planet_ttypedef struct{ char name[20]; double diameter; int moons; double orbit_time, rotation_time; } planet_t;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-8 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-9 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 ReferenceTypeValue plnpplanet_t *Address of structure refers to current_planet *plnpplanet_tReal structure of current_planet (*plnp).diameterdouble &(*plnp).diameterdouble *Address of diameter of current_planet structure “&(*plnp).diameter“&(*plnp).diameter” is evaluated as shown in the following table.

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 indirect component selection operator ->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 = ; 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 UnionUnion is a data structure which represents only one of the component in the declaration. union hair_tE.g., 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.

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