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

Slides:



Advertisements
Similar presentations
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
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.
5.1Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2006 Pearson Education, Inc. All rights reserved Pointers.
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.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
(continue) © by Pearson Education, Inc. All Rights Reserved.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 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.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
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.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© 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. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 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)
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 7.7Pointer Expressions and Pointer Arithmetic Arithmetic operations can be performed on pointers –Increment/decrement pointer ( ++ or -- ) –Add an integer.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
Chapter 5 - Pointers and Strings
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Chapter 7 from “C How to Program"
Chapter 5 - Pointers and Strings
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
Chapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings
7 C Pointers.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
5.1 Introduction Pointers Powerful, but difficult to master
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 18 Pointers – Part II
Chapter 5 - Pointers and Strings
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
Presentation transcript:

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

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.

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

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.

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] location

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

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 ]

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 ]

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

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; cout << "Data items in original order\n"; for ( i = 0; i < arraySize; i++ ) 24 cout << setw( 4 ) << a[ i ]; bubbleSort( a, arraySize ); // sort the array 27 cout << "\nData items in ascending order\n"; for ( i = 0; i < arraySize; i++ ) 30 cout << setw( 4 ) << a[ i ]; 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 University11 36void bubbleSort( int *array, const int size ) 37{ 38 void swap( int * const, int * const ); for ( int pass = 0; pass < size - 1; pass++ ) for ( int j = 0; j < size - 1; j++ ) 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 Data items in ascending order

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

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

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

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

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’

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