Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series."— Presentation transcript:

1 Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University yingwu@ece.northwestern.edu ECE230 Lectures Series

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

3 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;

4 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”.

5 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’

6 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.

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

8 10010000 10020008 int *pa = new int [3];

9 10014006 10020008 int **ppa = new int* [3]; for(int k=0;k<3;k++) ppa[k] = new int [3]; 10020008 10090080 10010308 10010000 10010308 10014006 Question: (1) ppa[1] = ? (2) ppa[1][2] = ? (3) ppa + 2 = ?

10 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?

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

12 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); }

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

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

15 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; }

16 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

17 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

18 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 }; 23 24 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"; 28 29 for ( counter = 0; counter < arraySize; counter++ ) 30 cout << setw( 4 ) << a[ counter ]; 31 32 if ( order == 1 ) { 33 bubble( a, arraySize, ascending ); 34 cout << "\nData items in ascending order\n"; Notice the function pointer parameter.

19 35 } 36 else { 37 bubble( a, arraySize, descending ); 38 cout << "\nData items in descending order\n"; 39 } 40 41 for ( counter = 0; counter < arraySize; counter++ ) 42 cout << setw( 4 ) << a[ counter ]; 43 44 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 52 53 for ( int pass = 1; pass < size; pass++ ) 54 55 for ( int j = 0; j < size - 1; j++ ) 56 57 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; 64 65 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.

20 Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89 69 70bool 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 2 6 4 8 10 12 89 68 45 37 Data items in descending order 89 68 45 37 12 10 8 6 4 2


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

Similar presentations


Ads by Google