department of computer science data structures using c++ 2e chapter 3 pointers and array-based lists...

30
Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data Simple Structured Pointers

Upload: cristal-truitt

Post on 14-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science

Data Structures Using C++ 2E

Chapter 3Pointers and Array-Based Lists

C++ has three types of data

Simple Structured Pointers

Page 2: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science

Data Structures Using C++ 2E

Chapter 3Pointers and Array-Based Lists

Page 3: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 3 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables

Declaring pointer variables Specify data type of value stored in location

pointer variable points toGeneral syntax

dataType *identifier;

Asterisk symbol (*)Examples: int *p; and char *ch;

Page 4: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 4 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Address-of operator (&)Unary operatorReturns address of its operand

Dereferencing operator (*)Unary operator

Different from binary multiplication operator

Also known as indirection operatorRefers to object where the pointer points

(operand of the *)

Page 5: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 5 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Pointers and classesDot operator (.)

Higher precedence than dereferencing operator (*)

Member access operator arrow ( ->)Simplifies access of class or struct components

via a pointerWhat’s difference between struct & class?

SyntaxpointerVariableName -> classMemberName

Page 6: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 6 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Initializing pointer variables No automatic variable initialization in C++ Pointer variables must be initialized

If not initialized, they do not point to anything

Initialized using Constant value 0 (null pointer) Named constant NULL

Number 0 Only number directly assignable to a pointer variable

Page 7: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 7 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Some C++ matters

What’s the difference between a struct and a class?

What’s a memory leak?

Page 8: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 8 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Some examples involving pointers

Playing with Pointers Example 3-2 page 135 Example 3-3 page 142

Page 9: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 9 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Various List Data Types

Array lists Linked lists Doubly linked lists

Page 10: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 10 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Dynamic variables Variables created during program execution

Real power of pointers

Two operators new: creates dynamic variables delete: destroys dynamic variables Reserved words

Page 11: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 11 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Operator new Allocates single variable Allocates array of variables Syntax

new dataType;new dataType[intExp];

Allocates memory (variable) of designated type Returns pointer to the memory (allocated memory

address) Allocated memory: uninitialized

Page 12: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 12 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Operator delete Destroys dynamic variables Syntax

delete pointerVariable;delete [ ] pointerVariable;

Memory leak Memory space that cannot be reallocated

Dangling pointers Pointer variables containing addresses of deallocated

memory spaces Avoid by setting deleted pointers to NULL after delete

Page 13: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 13 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Operations on pointer variables Operations allowed

Assignment, relational operations; some limited arithmetic operations

Can assign value of one pointer variable to another pointer variable of the same type

Can compare two pointer variables for equality Can add and subtract integer values from pointer

variable Danger

Accidentally accessing other variables’ memory locations and changing content without warning

Page 14: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 14 Data Structures Using C++ 2E

The Pointer Data Type and Pointer Variables (cont’d.)

Page 15: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 15

Text

Page 16: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 16

Text

Page 17: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 17

Text

Page 18: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 18

Text

Page 19: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 19

Text

Page 20: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 20

Text

Page 21: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 21

Text

Page 22: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 22

Text

Page 23: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 23

Text

Page 24: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 24

Text

Page 25: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 25

Text

Page 26: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 26

Text

Page 27: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 27

Text

Page 28: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 28

Text

Page 29: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 29

Text

Page 30: Department of Computer Science Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists C++ has three types of data  Simple  Structured

Department of Computer Science 30

Text