Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)

Similar presentations


Presentation on theme: "C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)"— Presentation transcript:

1 C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)

2 The Hashemite University2 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. Arrays of pointers. Examples.

3 The Hashemite University3 sizeof Operator and Arrays sizeof 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);

4 The Hashemite University4 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.

5 The Hashemite University5 Pointer Expressions and Pointer Arithmetic I Pointer arithmetic 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 int s 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] 30003004300830123016 location

6 The Hashemite University6 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 Pointers assignment Pointers of the same data type can be assigned to each other. If not the same type, a special cast operator must be used, otherwise it will result in a syntax error (no implicit casting for pointers exist).

7 The Hashemite University7 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 ]

8 The Hashemite University8 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 ]

9 The Hashemite University9 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

10 The Hashemite University10 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 5 6using std::cout; 7using std::endl; 8 9#include 10 11using std::setw; 12 13void bubbleSort( int *, const int ); 14 15int 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++ ) 24 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++ ) 30 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.

11 The Hashemite University11 36void 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 42 for ( int j = 0; j < size - 1; j++ ) 43 44 if ( array[ j ] > array[ j + 1 ] ) 45 swap( &array[ j ], &array[ j + 1 ] ); 46} 47 48void 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 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

12 The Hashemite University12 Example I // Converting lowercase letters to uppercase letters // using a non-constant pointer to non-constant data. #include #include // 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

13 The Hashemite University13 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

14 The Hashemite University14 Example II // Copying a string using array notation and pointer notation. #include 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

15 The Hashemite University15 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++ ) ; // do nothing in body } // end function copy2

16 The Hashemite University16 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 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’

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


Download ppt "C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)"

Similar presentations


Ads by Google