data structures using c++1 chapter 3 pointers dr. liu

49
Data Structures Using C++ 1 Chapter 3 Pointers Dr. Liu

Upload: theodore-lester

Post on 17-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 1

Chapter 3

Pointers

Dr. Liu

Page 2: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 2

Chapter Objectives

• Learn about the pointer data type and pointer variables

• Explore how to declare and manipulate the pointer variables

• Learn about the address of operator and dereferencing operator

• Discover dynamic variables• Examine how to use the new and delete operators

to manipulate dynamic variables

Page 3: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 3

Chapter Objectives

• Learn about pointer arithmetic• Discover dynamic arrays• Become aware of the shallow and deep

copies of data• Discover the peculiarities of classes with

pointer data members• Explore how dynamic arrays are used to

process lists

Page 4: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 4

What are pointers ?

• Variables that store memory address

•How to De-reference pointers

•How to do Pointer assignment

•How to use address-of operator

Page 5: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 5

What are pointers ?

5

3

8

1000

a

b

c

p

1000

1004

1008

1012

Address

int *p; int a = 5;p = &a; what is *p now ?;*p = *p + b; int c = *p;

Page 6: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 6

Pointer Data Types and Pointer Variables

• Pointer variable: variable whose content is a memory address

• Syntax to declare pointer variable:dataType *identifier; e.g. int *p;

• Address of operator: Ampersand, &, e.g. &a

• Dereferencing operator: Asterisk, *

Page 7: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 7

Pointers

• Statements:

int *p;

int num;

Page 8: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 8

Pointers

Page 9: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 9

Pointers

Page 10: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 10

Pointers

Page 11: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 11

Pointers

• Summary of preceding diagrams– &p, p, and *p all have different meanings– &p means the address of p– p means the content of p– *p means the content pointed to by p, that is

pointed to by the content of memory location

Page 12: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 12

Pointers

Page 13: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 13

Pointersx = 50;

p = &x;

Page 14: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 14

Pointers

*p = 38;

Page 15: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 15

Example 1

• // This program passes the address of a variable to a

• // function, which triples the value pointed to.

• Pointer example demo 1

• void triple_it(int *n);

Page 16: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 16

Example 2

• // This program sorts an array from hightest member

• // down to lowest, in that order.

• Pointer example demo 2.

• void swap(int *p1, int *p2)

Page 17: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 17

Example 3• void zero_out_array(int *arr, int n); void

zero_out_array(int *p, int n) {

• while (n-- > 0) { // Do n times:

• *p = 0; // Assign 0 to element // pointed to by p.

• p++; // Point to next element.

• }

• }

Page 18: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 18

Classes, structs, and Pointer Variables

The syntax for accessing a class (struct) member using the operator -> is

pointerVariableName->classMemberName

Therefore, the statement

(*studentPtr).gpa = 3.9;

is equivalent to the statement

studentPtr->gpa = 3.9;

Page 19: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 19

Classes, structs, and Pointer Variables

Page 20: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 20

Classes, structs, and Pointer Variables

Page 21: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 21

Syntax to use operator new

new dataType; //to allocate a single variable

new dataType[intExp]; //to allocate an array of variables

Page 22: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 22

Syntax to use operator delete

delete pointer; //to destroy a single dynamic variable

delete [] pointer; //to destroy a dynamically created array

Page 23: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 23

Operations on pointer variables

• Assignment operations

• Relational operations

• Limited arithmetic operations

Page 24: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 24

Functions and Pointers

void example(int* &p, double *q)

{

.

.

.

}

Page 25: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 25

Functions and Pointersvoid example(int* &p, double *q)

{

.

.

.

}

Page 26: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 26

Pointers and Function Return Pointers

int* testExp(...)

{

.

.

.

}

is a pointer of the type int.

Page 27: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 27

#include <stdio.h>

#define PRINTEM printf("%3d%5d%5d%5d%5d%5d\n", line++, num, x[0],x[1],x[2], ptr-x)

int main() {

static int x[] = {5,10,15}, lim =99;

int *ptr = x, num = 0;

int line = 0;

printf("LINE num x[0] x[1] x[2] ptr-x \n");

PRINTEM;

printf("\n\n");

num = *ptr; PRINTEM;

num = *++ptr; PRINTEM;

num = *ptr++; PRINTEM;

num = ++*ptr; PRINTEM;

num = (*ptr)++; PRINTEM;

num = ++(*ptr); PRINTEM;

num = *ptr + 1; PRINTEM;

num = *(ptr + 1); PRINTEM;

return 0;

}

Page 28: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 28

Shallow Versus Deep Copy and Pointers

Page 29: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 29

Shallow Versus Deep Copy and Pointers

Page 30: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 30

Shallow Versus Deep Copy and Pointers

second = new int[10];

for(int j = 0; j < 10; j++)

second[j] = first[j];

Page 31: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 31

Classes and Pointers: Some Peculiarities

class pointerDataClass

{

public:

...

private:

int x;

int lenP;

int *p;

};

pointerDataClass objectOne;

pointerDataClass objectTwo;

Page 32: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 32

Classes and Pointers: Some Peculiarities

Page 33: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 33

Destructor

Page 34: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 34

Destructor

pointerDataClass::~pointerDataClass()

{

delete [ ] p;

}

class pointerDataClass{public: ~pointerDataClass();...private: int x; int lenP; int *p;};

Page 35: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 35

Assignment Operator

Page 36: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 36

Assignment Operator

Page 37: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 37

Assignment Operator

Page 38: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 38

Overloading the Assignment Operator

Function Prototype (to be included in the definition of the class):const className& operator=(const className&);

Function Definition:const className& className::operator=(const className&

rightObject){ //local declaration, if any if(this != &rightObject) //avoid self-assignment { //algorithm to copy rightObject into this object } //return the object assigned return *this;}

Page 39: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 39

Overloading the Assignment Operator

• Definition of function operator=:– Only one formal parameter– Formal parameter generally const reference to

particular class– Return type of function is reference to

particular class

Page 40: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 40

Copy Constructor

Page 41: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 41

Copy Constructor

Page 42: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 42

Copy Constructor

Page 43: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 43

Copy Constructor

• If a class has pointer data members:– During object declaration, the initialization of one

object using the value of another object would lead to a shallow copying of the data if the default memberwise copying of data is allowed

– If, as a parameter, an object is passed by value and the default member-wise copying of data is allowed, it would lead to a shallow copying of the data

Page 44: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 44

Copy Constructor

• The copy constructor automatically executes in the following situations– When an object is declared and initialized by

using the value of another object– When, as a parameter, an object is passed by

value– When the return value of a function is an object

Page 45: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 45

Copy Constructor

Page 46: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 46

Copy Constructor

General syntax to include the copy constructor in the definition of a class:

className(const className& otherObject);

Page 47: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 47

Classes with Pointer Data Members

• Include destructor in the class

• Overload assignment operator for class

• Include copy constructor

Page 48: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 48

Overloading Array Index (Subscript) Operator ([ ])

Syntax to declare the operator function operator [ ] as a member of a class for nonconstant arrays:

Type& operator[](int index);

Syntax to declare the operator function operator [ ] as a member of a class for constant arrays:

const Type& operator[](int index) const;

Page 49: Data Structures Using C++1 Chapter 3 Pointers Dr. Liu

Data Structures Using C++ 49

Chapter Summary

• Pointer data types and variables

• Dynamic variables

• Pointer arithmetic

• Dynamic arrays

• Shallow and deep copying

• Peculiarities of classes with pointer data members