Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.

Slides:



Advertisements
Similar presentations
CHP-5 LinkedList.
Advertisements

Chapter 6 Data Types
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 10.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
C++ for Engineers and Scientists Third Edition
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
C++ for Engineers and Scientists Third Edition
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Microsoft Visual C++.NET Chapter 61 Memory Management.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
計算機程式語言 Lecture 13-1 國立臺灣大學生物機電系 13 Structures.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
POINTERS.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Data Structure & Algorithms
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointers and References
Chapter 10: Pointers 1.
Lecture 18 Arrays and Pointer Arithmetic
Objectives You should be able to describe: Addresses and Pointers
Pointers and References
Presentation transcript:

Chapter 13: Structures

In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists – Dynamic data structure allocation – Unions – Common programming errors Objectives C++ for Engineers and Scientists, Fourth Edition

Creating and using a structure involves two steps – Declare record structure – Assign specific values to structure elements Declaring a structure requires listing data types, data names, and arrangement of data items Data items, or fields, are called members of a structure Assigning data values to members is called populating the structure Single Structures C++ for Engineers and Scientists, Fourth Edition

An example structure definition: Single Structures (continued) C++ for Engineers and Scientists, Fourth Edition

The real power of structures is realized when the same structure is used for lists of data Arrays of Structures C++ for Engineers and Scientists, Fourth Edition Figure 13.1 A list of employee data

Arrays of Structures (continued) C++ for Engineers and Scientists, Fourth Edition Figure 13.2 A list of structures

Structure members can be passed to a function just like any scalar variable Given the structure emp definition: Pass a copy of emp.idNum to display() function: display(emp.idNum); Structures as Function Arguments C++ for Engineers and Scientists, Fourth Edition

Copies of all structure members can be passed to a function by including the name of the structure as an argument to a called function – Example: calcNet(emp) Structures as Function Arguments (continued) C++ for Engineers and Scientists, Fourth Edition

Instead of passing references, pointers can be used Function call must take the address of the structure – Example: calcNet(&emp); Function declaration must indicate a pointer argument – Example: calcNet(Employee *pt) Inside function, pt argument is used to reference members directly – Example: (*pt).idNum Passing a Pointer C++ for Engineers and Scientists, Fourth Edition

Passing a Pointer (continued) C++ for Engineers and Scientists, Fourth Edition Figure 13.3 A pointer can be used to access structure members

Using pointers to functions is very common Special notation exists for locating a member of a structure from a structure pointer – Notation is pointer->member Equivalent to (*pointer).member – Examples: Passing a Pointer (continued) C++ for Engineers and Scientists, Fourth Edition

Passing a Pointer (continued) C++ for Engineers and Scientists, Fourth Edition Figure 13.4 Changing pointer addresses

In practice, most structure-handling functions get direct access to a structure by receiving a structure reference or address – Changes to a structure can be made directly from a function To have a function return a separate structure, follow same procedure as for returning scalar values Returning Structures C++ for Engineers and Scientists, Fourth Edition

A classic data handling problem is making additions or deletions to existing structures that are maintained in a specific order Linked lists provide method for maintaining a constantly changing list Linked list: Set of structures in which each structure contains at least one member whose value is the address of next logically ordered structure in list Linked Lists C++ for Engineers and Scientists, Fourth Edition

Instead of requiring each record to be physically stored in correct order, each new structure is physically added wherever computer has free storage space Records are “linked” by including address of next record in the record immediately preceding it Current record contains address of next record no matter where next record is stored Linked Lists (continued) C++ for Engineers and Scientists, Fourth Edition

Linked Lists (continued) C++ for Engineers and Scientists, Fourth Edition Figure 13.5 Using pointers to link structures

Linked Lists (continued) C++ for Engineers and Scientists, Fourth Edition Figure 13.6 Adjusting addresses to point to the correct structures

Linked Lists (continued) C++ for Engineers and Scientists, Fourth Edition Figure 13.7 Using initial and final pointer values

Linked Lists (continued) C++ for Engineers and Scientists, Fourth Edition Figure 13.8 Storing an address in a structure member

C++ for Engineers and Scientists, Fourth Edition Figure 13.9 The relationship between structures in Program 13.8

Dynamic structure allocation uses the new and delete operators Dynamic Structure Allocation C++ for Engineers and Scientists, Fourth Edition Table 13.1 Operators for Dynamic Allocation and Deallocation

Dynamic structure allocation permits expanding a list as new records are added and contracting the list as records are deleted When dynamically allocating storage, new operator must be provided with amount of space to allocate new returns a pointer to the storage just allocated Dynamic Structure Allocation (continued) C++ for Engineers and Scientists, Fourth Edition

Union: Data type that reserves same area in memory for two or more variables that can be different data types Definition of union has same form as structure definition – Keyword union replaces keyword struct Unions C++ for Engineers and Scientists, Fourth Edition

Example: Union val contains a single member that can be character variable named key, integer named num, or double-precision variable named volts Unions (continued) C++ for Engineers and Scientists, Fourth Edition

Typically, second variable is used to keep track of current data type stored in union C++ for Engineers and Scientists, Fourth Edition Unions (continued)

Example: Referencing union data member based on type indicator member C++ for Engineers and Scientists, Fourth Edition Unions (continued)

Structures or unions cannot be used in relational expressions When pointer is used to point to a structure or a union, take care to use address in pointer to point to correct data type Be careful to keep track of the data stored in a union – Accessing data in a union by the wrong variable name is a troublesome error to locate Common Programming Errors C++ for Engineers and Scientists, Fourth Edition

Structure allows grouping variables under a common variable name – Each variable in structure accessed by structure name, followed by period, followed by variable name One form for declaring a structure is: Summary C++ for Engineers and Scientists, Fourth Edition

A data type can be created from a structure by using this declaration form: Summary (continued) C++ for Engineers and Scientists, Fourth Edition

Structures are particularly useful as elements of arrays – Each structure becomes a record in a list of records Complete structures can be used as function arguments, in which case called function receives a copy of the structure elements – Structure addresses can also be passed as a reference or a pointer – When passed as pointer or reference, function has access to change original structure member values Summary (continued) C++ for Engineers and Scientists, Fourth Edition

Structure members can be any valid C++ data type – May be other structures, unions, arrays, or pointers When pointer included in structure, a linked list can be created Unions are declared in same manner as structures – Definition of union creates a memory overlay area – Each union member uses the same memory storage locations Summary (continued) C++ for Engineers and Scientists, Fourth Edition