Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Lecture 18 Pointers – Part II

Similar presentations


Presentation on theme: "C++ Programming Lecture 18 Pointers – Part II"— Presentation transcript:

1 C++ Programming Lecture 18 Pointers – Part II
By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

2 The Hashemite University
Outline Introduction. sizeof Operator and Arrays. sizeof Operator and Pointers. Bubble Sort Using Call-by-reference. Pointer Expressions and Pointer Arithmetic. The Relationship Between Pointers and Arrays. Examples. The Hashemite University

3 sizeof Operator and Arrays
Returns size of operand in bytes Returns the result of type size_t which is unsigned integer. For arrays, sizeof returns ( the size of 1 element ) * ( number of elements ) if sizeof( int ) = 4, then int myArray[10]; cout << sizeof(myArray); will print 40 To get the size of an array (number of elements) using sizeof operator do the following: Array size = sizeof(myArray)/ sizeof(int); The Hashemite University

4 sizeof Operator and Pointers
Applying sizeof operator for a pointer always returns a result of 4 regardless of the data type to which the pointer is pointing. The Hashemite University

5 Bubble Sort Using Call-by-reference
We will implement bubblesort function using pointers swap function must receive the address (using &) of the array elements array elements have call-by-value default Using pointers and the * operator, swap is able to switch the values of the actual array elements Psuedocode Initialize array print data in original order Call function bubblesort print sorted array Define bubblesort The Hashemite University

6 The Hashemite University
1 // Fig. 5.15: fig05_15.cpp 2 // This program puts values into an array, sorts the values into 3 // ascending order, and prints the resulting array. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 #include <iomanip> 10 11 using std::setw; 12 13 void bubbleSort( int *, const int ); 14 15 int main() 16 { 17 const int arraySize = 10; 18 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 19 int i; 20 21 cout << "Data items in original order\n"; 22 23 for ( i = 0; i < arraySize; i++ ) cout << setw( 4 ) << a[ i ]; 25 26 bubbleSort( a, arraySize ); // sort the array 27 cout << "\nData items in ascending order\n"; 28 29 for ( i = 0; i < arraySize; i++ ) cout << setw( 4 ) << a[ i ]; 31 32 cout << endl; 33 return 0; 34 } Bubblesort gets passed the address of array elements (pointers). The name of an array is a pointer. The Hashemite University

7 The Hashemite University
36 void bubbleSort( int *array, const int size ) 37 { 38 void swap( int * const, int * const ); 39 40 for ( int pass = 0; pass < size - 1; pass++ ) 41 for ( int j = 0; j < size - 1; j++ ) 43 if ( array[ j ] > array[ j + 1 ] ) swap( &array[ j ], &array[ j + 1 ] ); 46 } 47 48 void swap( int * const element1Ptr, int * const element2Ptr ) 49 { 50 int hold = *element1Ptr; 51 *element1Ptr = *element2Ptr; 52 *element2Ptr = hold; 53 } swap takes pointers (addresses of array elements) and dereferences them to modify the original array elements. Data items in original order Data items in ascending order The Hashemite University

8 Pointer Expressions and Pointer Arithmetic I
Increment/decrement pointer (++ or --) Add/subtract an integer to/from a pointer( + or += , - or -=) Pointers may be subtracted from each other Pointer arithmetic is meaningless unless performed on an array 5 element int array on a machine using 4 byte ints vPtr points to first element v[ 0 ], which is at location 3000 vPtr = 3000 vPtr += 2; sets vPtr to 3008 vPtr points to v[ 2 ] pointer variable vPtr v[0] v[1] v[2] v[4] v[3] 3000 3004 3008 3012 3016 location The Hashemite University

9 Pointer Expressions and Pointer Arithmetic II
Subtracting pointers Returns the number of elements between two addresses vPtr2 = &v[ 2 ]; vPtr = &v[ 0 ]; vPtr2 - vPtr = 2 Pointer comparison Test which pointer points to the higher numbered array element Test if a pointer points to 0 (NULL) if ( vPtr == NULL ) statement The Hashemite University

10 Pointer Expressions and Pointer Arithmetic III
Pointers assignment If not the same type, a cast operator must be used Exception: pointer to void (type void *) Generic pointer, represents any type No casting needed to convert a pointer to void pointer void pointers cannot be dereferenced Example: #include<iostream.h> int main() { int x, y; void*ptr = &x; int *pptr = &y; ptr = pptr; pptr = (int*)ptr; //casting } The Hashemite University

11 The Relationship Between Pointers and Arrays I
Arrays and pointers are closely related Array name is a constant pointer that contains the address of the first element in the array. Pointers can do array subscripting operations Having declared an array b[ 5 ] and a pointer bPtr bPtr is equal to b bptr == b bptr is equal to the address of the first element of b bptr == &b[ 0 ] The Hashemite University

12 The Relationship Between Pointers and Arrays II
Accessing array elements with pointers Element b[ n ] can be accessed by *( bPtr + n ) Called pointer/offset notation Array itself can use pointer arithmetic. b[ 3 ] same as *(b + 3) Pointers can be subscripted (pointer/subscript notation) bPtr[ 3 ] same as b[ 3 ] The Hashemite University

13 The Hashemite University
Example I // Converting lowercase letters to uppercase letters // using a non-constant pointer to non-constant data. #include <iostream.h> #include <cctype> // prototypes for islower and toupper void convertToUppercase( char * ); int main() { char phrase[] = "characters and $32.98"; cout << "The phrase before conversion is: " << phrase; convertToUppercase( phrase ); cout << "\nThe phrase after conversion is: “ << phrase << endl; return 0; } // end main The Hashemite University

14 The Hashemite University
Example I … cont. // convert string to uppercase letters void convertToUppercase( char *sPtr ) { while ( *sPtr != '\0' ) { // current character is not '\0' if ( islower( *sPtr ) ) // if character is lowercase, *sPtr = toupper( *sPtr ); // convert to uppercase ++sPtr; // move sPtr to next character in string } // end while } // end function convertToUppercase The Hashemite University

15 The Hashemite University
Example II // Copying a string using array notation and pointer notation. #include <iostream.h> void copy1( char *, const char * ); // prototype void copy2( char *, const char * ); // prototype int main() { char string1[ 10 ]; char *string2 = "Hello"; char string3[ 10 ]; char string4[] = "Good Bye"; copy1( string1, string2 ); cout << "string1 = " << string1 << endl; copy2( string3, string4 ); cout << "string3 = " << string3 << endl; return 0; // indicates successful termination } // end main The Hashemite University

16 The Hashemite University
Example II … cont. // copy s2 to s1 using array notation void copy1( char *s1, const char *s2 ) { for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) ; // do nothing in body } // end function copy1 // copy s2 to s1 using pointer notation void copy2( char *s1, const char *s2 ) for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) } // end function copy2 The Hashemite University

17 The Hashemite University
Additional Notes This lecture covers the following material from the textbook: Chapter 5: Sections 5.6 – 5.8 The Hashemite University


Download ppt "C++ Programming Lecture 18 Pointers – Part II"

Similar presentations


Ads by Google