Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Advertisements

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Array, Pointer and Reference ( III ) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
1 Array, Pointer and Reference ( IV ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
1 Dynamic Memory Allocation Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
Esempi Puntatori e Stringhe1 // This program puts values into an array, sorts the values into // ascending order, and prints the resulting array. #include.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Examples Using Arrays Passing arrays to functions Class GradeBook: store student grades.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Review slides Mar 15, 2005.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
IS Program Design and Software Tools Introduction to C++ Programming
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 5 - Pointers and Strings
Chapter 7 - Pointers Outline 7.1 Introduction
C++ Programming Lecture 15 Arrays – Part II
CSC 113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Pointers and Strings
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
C++ Programming Lecture 15 Arrays – Part II
Remark: Data Type of Array Name
Arrays Kingdom of Saudi Arabia
Chapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Presentation transcript:

Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series

What to learn today? Advanced topics on pointers –Array of pointers –2D pointers –Function pointers This is really your last chance of grasping pointers

What does it mean? If I declaim two pointers… int *p1; char *p2; What if … ? int *p1; p1 = new int [10]; p1[4] = 12; *(p1+5) = 2;

A Rule of thumb Before you can use a pointer or a reference, you MUST initialize it! What do you mean by “initialize”? –Let it point to a place where the memory is determined –Otherwise, the pointer is “wild”.

Arrays of Pointers Arrays can contain pointers –Commonly used to store an array of strings char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" }; –Each element of suit is a pointer to a char * (a string) –The strings are not in the array, only pointers to the strings are in the array –Indexing: suit[0][3]=? –suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0]’H’’e’’a’’r’’t’’s’ ’\0’ ’D’’i’’a’’m’’o’’n’’d’’s’ ’\0’ ’C’’l’’u’’b’’s’ ’\0’ ’S’’p’’a’’d’’e’’s’ ’\0’

2D pointers Important Concepts int *p1; –p1 is a pointer –What does p1 point to? Can be an integer Can be an integer array and we can index by p1[n]. Here p1[n] is an integer int **p2; –p2 is a pointer –What does p2 point to? Can be a pointer pointing to an integer Can be an array of pointers and we can index by p2[n]. Here p2[n] is a pointer! Therefore, p2[n] can point to an integer or an integer array.

int a = 8; int *pa = &a; int **ppa = &pa; Question: [1] (*pa) = ? [2] (**ppa) = ?

int *pa = new int [3];

int **ppa = new int* [3]; for(int k=0;k<3;k++) ppa[k] = new int [3]; Question: (1) ppa[1] = ? (2) ppa[1][2] = ? (3) ppa + 2 = ?

Example: Table I want to implement a general table class, which holds any size of row/column. How can I construct such a table? What should we do for the constructor and destructor?

class CTable() { int **m_pData; int m_nRow; int m_nCol; public: CTable(int r = 1, int c = 1); CTable(const CTable &t); ~CTable(); … };

CTable::CTable(int r, int c) { assert(r>0 && c>0); m_nRow = r; m_nCol = c; m_pData = new int *[m_nRow]; assert(m_pData != NULL); for(int k=0; k<m_nRow; k++){ m_pData[k] = new int [m_nCol]; assert(m_pData[k] != NULL); }

CTable::~CTable() { for(int k=0; k<m_nRow; k++) delete [] m_nData[k]; delete [] m_nData; }

Bubble Sort Example: –Original: –Pass 1: –Pass 2: –Small elements "bubble" to the top –Worst case: Original: Pass 1: ; ; ; ; Pass 2: ; ; ; Pass 3: ; ; Pass 4:

void bubbleSort( int data[], int size) { for ( int pass = 1; pass < size; pass++ ){ for ( int j = 0; j < size - 1; j++ ){ if ( data[ j ] > data[ j + 1 ] ) { swap(data[j], data[j+1]); // swap two elements } void swap(int &a, int &b) { int c; c = a; a = b; b = c; }

Function Pointers Pointers to functions –Contain the address of the function –Similar to how an array name is the address of its first element –Function name is starting address of code that defines function Function pointers can be –Passed to functions –Stored in arrays –Assigned to other function pointers

Function Pointers Example: bubblesort –Function bubble() takes a function pointer The function determines whether the the array is sorted into ascending or descending sorting –The argument in bubble for the function pointer bool ( *compare )( int, int ) tells bubble() to expect a pointer to a function that takes two int s and returns a bool –NOTE: but if the parentheses were left out bool *compare( int, int ) would declare a function that receives two integers and returns a pointer to a bool

1. Initialize array 2. Prompt for ascending or descending sorting 2.1 Put appropriate function pointer into bubblesort 2.2 Call bubble 3. Print results 1// Fig. 5.26: fig05_26.cpp 2// Multipurpose sorting program using function pointers 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9#include 10 using std::setw; 11 12void swap( int&, int& ); 13void bubble( int [], const int, bool (*)( int, int ) ); 14bool ascending( int, int ); 15bool descending( int, int ); 16 17int main() 18{ 19 const int arraySize = 10; 20 int order, 21 counter, 22 a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; cout << "Enter 1 to sort in ascending order,\n" 25 << "Enter 2 to sort in descending order: "; 26 cin >> order; 27 cout << "\nData items in original order\n"; for ( counter = 0; counter < arraySize; counter++ ) 30 cout << setw( 4 ) << a[ counter ]; if ( order == 1 ) { 33 bubble( a, arraySize, ascending ); 34 cout << "\nData items in ascending order\n"; Notice the function pointer parameter.

35 } 36 else { 37 bubble( a, arraySize, descending ); 38 cout << "\nData items in descending order\n"; 39 } for ( counter = 0; counter < arraySize; counter++ ) 42 cout << setw( 4 ) << a[ counter ]; cout << endl; 45 return 0; 46} 47 48void bubble( int data[], const int size, 49 bool (*compare)( int, int ) ) 50{ 51 // the following uses functions pointers for ( int pass = 1; pass < size; pass++ ) for ( int j = 0; j < size - 1; j++ ) if ( (*compare)( data[j], data[j+1 ] ) ) 58 swap( data[j], data[j+1] ); 59} 60 61void swap( int& a, int& b ) 62{ 63 int c; c = a; 66 a = b; 67 b = c; 68} ascending and descending return true or false. bubble calls swap if the function call returns true. Notice how function pointers are called using the dereferencing operator. The * is not required, but emphasizes that compare is a function pointer and not a function.

Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in original order Data items in ascending order bool ascending( int a, int b ) 71{ 72 return b < a; // swap if b is less than a 73} 74 75bool descending( int a, int b ) 76{ 77 return b > a; // swap if b is greater than a 78} Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2 Data items in original order Data items in descending order