Lecture 25:Pointers - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 25:Pointers

Description:

Sequential Search on an Ordered List. General form of sequential ... Heap based memory allocation. We skip these issues because time limit. End of lecture 23 ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 23
Provided by: xw
Category:
Tags: heap | lecture | pointers

less

Transcript and Presenter's Notes

Title: Lecture 25:Pointers


1
Lecture 25Pointers
  • 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
Sequential Search on an Ordered List
int seqOrderedSearch(const int list, int
listLength, int searchItem) int
loc //Line 1 bool found false //Line
2 for (loc 0 loc lt listLength
loc) //Line 3 if (listloc gt
searchItem) //Line 4 found
true //Line 5 break //Line 6
if (found) //Line 7 if
(listloc searchItem) //Line 8 return
loc //Line 9 else //Line 10
return -1 //Line 11 else //Line 12
return -1 //Line 13
6
Binary Search
  • Binary search can be applied to sorted lists
  • Uses the divide and conquer technique
  • Compare search item to middle element
  • mid (firstlast)/2
  • first is the starting index of the search list
  • last is the ending index of the search list
  • mid 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
8
Binary Search
int binarySearch(const int list, int
listLength, int searchItem) int first
0 int last listLength - 1 int mid bool
found false while(first lt last
!found) mid (first last) /
2 if(listmid searchItem) found
true else if(listmid gt searchItem)
last mid - 1 else first mid
1 if(found) return mid else
return -1
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
num78
p num
p 24
16
An application of pointer parameter passed by
address
  • void swap(int x, int y)
  • int tmpx
  • xy
  • ytmp
  • int a1, b2
  • swap(a, b)

a
b
1
2
2
1
x
y
tmp1
17
Difference between pointers and references
  • Reference is always the alias of some variable,
    but pointer does not have to be.
  • int a int raa
  • 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 raa rab //ra is not the alias
    of b. Here, rab is equivalent to ab
  • int pax pay //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
  • pp1 or p //assume p is a pointer
  • Subtract a number
  • pp-1 or p-- //assume p is a pointer
  • Taken the difference of two pointers
  • int dp1-p2 //assume p1, p2 are pointers

19
The meaning of Pointer Arithmetic
q
p
p3
p-2
q-p5
20
Another application of pointeraccess array by
using pointer
int A10
100
p
int p A0
(p4)100
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!
Write a Comment
User Comments (0)
About PowerShow.com