Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.

Slides:



Advertisements
Similar presentations
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
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.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
C and Data Structures Baojian Hua
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Array, Structure and Union
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
C Programming Lecture 11 : Structure. Data Type struct union enum typedef.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CPS120: Introduction to Computer Science Lecture 15A Structures.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
CPS120: Introduction to Computer Science Data Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
Lecture 9 Structure 1. Concepts of structure Pointers of structures
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Structures, Unions, and Typedefs
CS111 Computer Programming
Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next
Structures CSE 2031 Fall February 2019.
C Structures, Unions, Bit Manipulations and Enumerations
C Programming Pointers
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
CS1100 Computational Engineering
Structures EECS July 2019.
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2Ku-Yaw ChangStructures Structures A structure A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Also called records in some language, notably Pascal Also called records in some language, notably Pascal Examples Examples An employee is described by a set of attributes Name Name Address Address Social security number Social security number Salary Salary A point is a pair of coordinates A rectangle is a pair of points A rectangle is a pair of points

3Ku-Yaw ChangStructures 6.1 Basics of Structures A point x coordinate (integer) x coordinate (integer) y coordinate (integer) y coordinate (integer) The above two components can be placed in a structure declared like this: struct point { int x; int y; }; A structure tag A structure tag an optional name: point Members Members x and y

4Ku-Yaw ChangStructures 6.1 Basics of Structures More examples struct card { char * face; char * suit; }; struct card { char * face; char * suit; }; struct employee { char firstName[20]; char lastName[20]; int age; char gender; double hourlySalary; struct employee person; /* error*/ struct employee * ePter; /* pointer */ };

5Ku-Yaw ChangStructures 6.1 Basics of Structures A structure declaration Reserve no storage (memory) Reserve no storage (memory) Structure variables are defined like variables of other types struct point pt; struct point pt; struct card aCard, deck[52], * cardPtr; struct card aCard, deck[52], * cardPtr; struct card { char * face; char * suit; } aCard, deck[52], * cardPtr; struct card { char * face; char * suit; } aCard, deck[52], * cardPtr;

6Ku-Yaw ChangStructures 6.1 Basics of Structures A member of a particular structure is referred to in an expression by a construction of the form – dot operator structure-name. member For example, To print the coordinates of the point pt To print the coordinates of the point pt printf(“%d, %d”, pt.x, pt.y); To compute the distance from the origin (0,0) to pt To compute the distance from the origin (0,0) to pt double dist, sqrt(double); dist = sqrt( (double)pt.x * pt.x + (double)pt.y * pt.y)

7Ku-Yaw ChangStructures 6.1 Basics of Structures A member of a particular structure can also be referred by arrow operator For example, struct card aCard, * cardPtr; cardPtr = &aCard; printf(“%s”, cardPrt->suit); struct card aCard, * cardPtr; cardPtr = &aCard; printf(“%s”, cardPrt->suit);

8Ku-Yaw ChangStructures 6.1 Basics of Structures Structures can be nested struct rect { struct point pt1; struct point pt2; }; struct rect { struct point pt1; struct point pt2; }; For example, struct rect screen; screen.pt1.x struct rect screen; screen.pt1.x

9Ku-Yaw ChangStructures 6.7 typedef typedef Create new data type names Create new data type names typedef int Length; Make the name Length a synonym for int Make the name Length a synonym for int Length len, maxlen; Length len, maxlen; typedef char * String; Make String a synonym for char * or character pointer Make String a synonym for char * or character pointer String p; String p; Typedef struct tnode * Treeptr;

10Ku-Yaw ChangStructures 6.8 Unions A union A variable that may hold (at different times) objects of different types and sizes A variable that may hold (at different times) objects of different types and sizes Provide a way to manipulate different kinds of data in a single area of storage Provide a way to manipulate different kinds of data in a single area of storage Example Example union u_tag { int ival; floatfval; char *sval; } u; The variable u will be large enough to hold the largest of three types.

To be continued…