Title: Lecture 25:Pointers
1Lecture 25Pointers
- Introduction to Computer Science
- Spring 2006
2Contents
- Review searching algorithm on an ordered list
- Pointers
3Contents
- Review searching algorithm on an ordered list
- Pointers
4Sequential Search on an Ordered List
- General form of sequential search algorithm on a
sorted list
5Sequential 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
6Binary 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
7Search 75 in the following list
8Binary 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
9Contents
- Review searching algorithm on an ordered list
- Pointers
10Three categories of data type
- Simple data type
- Structured data type
- Pointer
11Pointer 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
12Pointer 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
13Address 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
14Dereferencing 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
15int p int num
num78
p num
p 24
16An 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
17Difference 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.
18Pointer 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
19The meaning of Pointer Arithmetic
q
p
p3
p-2
q-p5
20Another application of pointeraccess array by
using pointer
int A10
100
p
int p A0
(p4)100
21Other 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.
22End of lecture 23