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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Data Structures Using C++ 2E
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
CHAPTER 10 ARRAYS II Applications and Extensions.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Pointers Applications
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Data Structures Using C++1 Chapter 9 Search Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers OVERVIEW.
CSC 211 Data Structures Lecture 13
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Object-Oriented Programming in C++
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 14: Searching and Sorting
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Data Structures Using C++
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Engineering Computing I Chapter 5 Pointers and Arrays.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
1 CSC103: Introduction to Computer and Programming Lecture No 17.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Windows Programming Lecture 03. Pointers and Arrays.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointer Data Type and Pointer Variables
Pointers Lecture 2 Tue, Jan 24, 2006.
CS150 Introduction to Computer Science 1
Data Structures and Algorithms Introduction to Pointers
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Presentation transcript:

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 11 else//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 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: search 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; 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 address void swap(int *x, int *y) { int tmp=*x; *x=*y; *y=tmp; } int a=1, b=2; swap(&a, &b); 1 ab 2 xy tmp=1 21

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!