1 Lecture 25:Pointers Introduction to Computer Science Spring 2006.

Post on 20-Dec-2015

214 views 2 download

Transcript of 1 Lecture 25:Pointers Introduction to Computer Science Spring 2006.

1

Lecture 25:Pointers

Introduction to Computer Science

Spring 2006

2

Contents

Review searching algorithm on an ordered list

Pointers

3

Contents

Review searching algorithm on an ordered list

Pointers

4

Sequential Search on an Ordered List

General form of sequential search algorithm on a sorted list:

5

int seqOrderedSearch(const int list[], int listLength, int searchItem){ int loc; //Line 1 bool found = false; //Line 2

for (loc = 0; loc < listLength; loc++) //Line 3 if (list[loc] >= searchItem) //Line 4 {

found = true; //Line 5 break; //Line 6

} if (found) //Line 7 if (list[loc] == searchItem) //Line 8

return loc; //Line 9 else //Line 10

return -1; //Line 11else //Line 12 return -1; //Line 13

}

Sequential Search on an Ordered List

6

Binary Search

Binary search can be applied to sorted lists Uses the “divide and conquer” technique

Compare search item to middle element mid= (first+last)/2

first is the starting index of the search listlast is the ending index of the search listmid is the index of the middle element of the search list

If search item is less than middle element, restrict the search to the lower half of the list

Otherwise search the upper half of the list

7

Search 75 in the following list:

search listsearch list

search listsearch list

8

int binarySearch(const int list[], int listLength, int searchItem){

int first = 0;int last = listLength - 1;int mid;

bool found = false;

while(first <= last && !found){

mid = (first + last) / 2;

if(list[mid] == searchItem)found = true;

else if(list[mid] > searchItem)

last = mid - 1; else

first = mid + 1;}

if(found) return mid;

else return -1;

}

Binary Search

9

Contents

Review searching algorithm on an ordered list

Pointers

10

Three categories of data type

Simple data type Structured data type Pointer

11

Pointer Variables int count = 5;

The value "5" is stored in memory and can be accessed by using the variable "count".

Pointer variable: content is a memory address

Declaring Pointer Variables

dataType *identifier;

int *p;

char *ch;

12

Pointer Variables (continued) These statements are equivalent

int *p; int* p; int * p;

The character * can appear anywhere between type name and variable name

int* p, q; Only p is the pointer variable, not q. Here q is an int variable

To avoid confusion, attach the character * to the variable name

int *p, q;

The following statement declares both p and q to be pointer variables of the type int

int *p, *q;

13

Address Of Operator (&)

Example:

int count = 5;

int *p;

The ampersand, &, is called the address of operator

p = &count;

The address of operator is a unary operator that returns the address of its operand

14

Dereferencing Operator (*) C++ uses * as the binary multiplication operator and as a

unary operator

When used as a unary operator, *

Called dereferencing operator or indirection operator

Refers to object to which its operand (that is, a pointer) points

Example:

int count = 5;

int *p;p = &count; // Stores the address of count in p

    // The unary operator & returns the address of a variable int total;

total = *p;     // The value in the address stored in p is assigned to total

15

int *p;

int num;num=78;

p = &num;

*p = 24;

16

An application of pointer: parameter passed by addressvoid swap(int *x, int

*y)

{

int tmp=*x;

*x=*y;

*y=tmp;

}

int a=1, b=2;

swap(&a, &b);

1

a b

2

x y

tmp=1

2 1

17

Difference between pointers and references

• Reference is always the alias of some variable, but pointer does not have to be.– int a; int &ra=a; – int *pa; //pa is not an alias of any variable, we call it dangling.

• Reference can not refer other variable after it is defined.– int a, b; int &ra=a; ra=b; //ra is not the alias of b. Here, ra=b is

equivalent to a=b– int *pa=&x; pa=&y; //pa refers to y now

• pointer supports arithmetic operation, reference does not.

• Conclusion: reference is more restricted, more safe than pointer. But Pointer is more powerful than reference.

18

Pointer Arithmetic Operations of Pointers:

Add a number p=p+1; or p++ //assume p is a pointer

Subtract a number p=p-1 or p--; //assume p is a pointer

Taken the difference of two pointers int d=p1-p2 //assume p1, p2 are pointers

19

The meaning of Pointer Arithmetic

p p+3p-2 q

q-p=5

20

Another application of pointer:access array by using pointer

100

int A[10];

int *p = &A[0];

*(p+4)=100;

p

21

Other applications of Pointers

Pointers of other data type Pointer of array, pointer of function,

pointer of pointer. Dynamic memory allocation

Heap based memory allocation.

We skip these issues because time limit.

22

End of lecture 23

Thank you!