2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5.4 Calling Functions by Reference 5.5 Using const with Pointers 5.6 Bubble Sort Using Pass-by-Reference 5.7 Pointer Expressions and Pointer Arithmetic 5.8 Relationship Between Pointers and Arrays 5.9 Arrays of Pointers 5.10 Case Study: Card Shuffling and Dealing Simulation 5.11 Function Pointers 5.12 Introduction to Character and String Processing 5.12.1 Fundamentals of Characters and Strings 5.12.2 String Manipulation Functions of the String- Handling Library 2003 Prentice Hall, Inc. All rights reserved. 2 5.1 Introduction • Pointers – Powerful, but difficult to master – Simulate pass-by-reference – Close relationship with arrays and strings 2003 Prentice Hall, Inc. All rights reserved. 3 5.2 Pointer Variable Declarations and Initialization • Pointer variables – Contain memory addresses as values – Normally, variable contains specific value (direct reference) – Pointers contain address of variable that has specific value (indirect reference) • Indirection – Referencing value through pointer • Pointer declarations – * indicates variable is pointer int *myPtr; declares pointer to int, pointer of type int * – Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; count 7 countPtr count 7 2003 Prentice Hall, Inc. All rights reserved. 4 5.2 Pointer Variable Declarations and Initialization • Can declare pointers to any data type • Pointer initialization – Initialized to 0, NULL, or address • 0 or NULL points to nothing 2003 Prentice Hall, Inc. All rights reserved. 5 5.3 Pointer Operators • & (address operator) – Returns memory address of its operand – Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y – yPtr “points to” y yPtr y 5 yptr 500000 600000 y 600000 5 address of y is value of yptr 2003 Prentice Hall, Inc. All rights reserved. 6 5.3 Pointer Operators • * (indirection/dereferencing operator) – Returns synonym for object its pointer operand points to – *yPtr returns y (because yPtr points to y). – dereferenced pointer is lvalue *yptr = 9; // assigns 9 to y • * and & are inverses of each other 2003 Prentice Hall, Inc. All rights reserved. Outline 7 fig05_04.cpp (1 of 2) 1 // Fig. 5.4: fig05_04.cpp 2 // Using the & and * operators. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int a; // a is an integer 11 int *aPtr; // aPtr is a pointer to an integer 12 13 a = 7; 14 aPtr = &a; // aPtr assigned address of a 15 16 cout << "The address of a is " << &a 17 << "\nThe value of aPtr is " << aPtr; 18 19 cout << "\n\nThe value of a is " << a 20 << "\nThe value of *aPtr is " << *aPtr; 21 22 cout << "\n\nShowing that * and & are inverses of " 23 << "each other.\n&*aPtr = " << &*aPtr 24 << "\n*&aPtr = " << *&aPtr << endl; 25 * and & are inverses of each other 2003 Prentice Hall, Inc. All rights reserved. Outline 8 fig05_04.cpp (2 of 2) fig05_04.cpp output (1 of 1) 26 return 0; // indicates successful termination 27 28 } // end main The address of a is 0012FED4 The value of aPtr is 0012FED4 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012FED4 *&aPtr = 0012FED4 * and & are inverses; same result when both applied to aPtr 2003 Prentice Hall, Inc. All rights reserved. 9 5.4 Calling Functions by Reference • 3 ways to pass arguments to function – Pass-by-value – Pass-by-reference with reference arguments – Pass-by-reference with pointer arguments • return can return one value from function • Arguments passed to function using reference arguments – Modify original values of arguments – More than one value “returned” 2003 Prentice Hall, Inc. All rights reserved. 10 5.4 Calling Functions by Reference • Pass-by-reference with pointer arguments – Simulate pass-by-reference • Use pointers and indirection operator – Pass address of argument using & operator – Arrays not passed with & because array name already pointer – * operator used as alias/nickname for variable inside of function [...]... © 2003 Prentice Hall, Inc All rights reserved 27 51 52 53 54 55 56 57 58 59 60 61 62 Outline } // end function bubbleSort // swap values at memory locations to which // element1Ptr and element2Ptr point void swap( int * const element1Ptr, int * const element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; Pass *element2Ptr = hold; fig 05_ 15. cpp output (1 of arguments by reference, 1) allowing... rights reserved 24 25 5.6 Bubble Sort Using Pass-by-Reference • Implement bubbleSort using pointers – Want function swap to access array elements • Individual array elements: scalars – Passed by value by default • Pass by reference using address operator & © 2003 Prentice Hall, Inc All rights reserved 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig 5. 15: fig 05_ 15. cpp // This program... d:\cpphtp4_examples\ch 05\ Fig 05_ 12.cpp(21) : error C2166: l-value specifies const object © 2003 Prentice Hall, Inc All rights reserved 20 21 5. 5 Using const with Pointers • const pointers – Always point to same memory location – Default for array name – Must be initialized when declared © 2003 Prentice Hall, Inc All rights reserved 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Fig 5. 13: fig 05_ 13.cpp // Attempting... rights reserved 11 25 26 27 28 29 30 // calculate and return cube of integer argument int cubeByValue( int n ) { cubeByValue receives return n * n * n; // cube local variable n and return result parameter passed-by-value } // end function cubeByValue The original value of number is 5 The new value of number is 1 25 Cubes and returns local variable n Outline fig 05_ 06.cpp (2 of 2) fig 05_ 06.cpp output (1... rights reserved 12 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig 5. 7: fig 05_ 07.cpp // Cube a variable using pass-by-reference // with a pointer argument #include using std::cout; using std::endl; void cubeByReference( int * ); Outline Prototype indicates parameter is pointer to int fig 05_ 07.cpp (1 of 2) // prototype int main() { int number = 5; Apply address operator... // indicates successful } // end main Line 15 generates compiler error by attempting to assign termination new address to constant pointer d:\cpphtp4_examples\ch 05\ Fig 05_ 13.cpp( 15) : error C2166: l-value specifies const object © 2003 Prentice Hall, Inc All rights reserved 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // Fig 5. 14: fig 05_ 14.cpp // Attempting to modify a constant pointer... function cubeByReference The original value of number is 5 The new value of number is 1 25 cubeByReference receives address of int variable, i.e., pointer to an int Outline fig 05_ 07.cpp (2 of 2) fig 05_ 07.cpp output (1 of 1) Modify and access int variable using indirection operator * © 2003 Prentice Hall, Inc All rights reserved 14 15 5 .5 Using const with Pointers • const qualifier – Value of variable should... reserved 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig 5. 10: fig 05_ 10.cpp // Converting lowercase letters to uppercase letters // using a non-constant pointer to non-constant data #include using std::cout; using std::endl; #include // prototypes for Outline fig 05_ 10.cpp (1 of 2) Parameter is nonconstant pointer to nonconstant data islower and toupper void...1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // Fig 5. 6: fig 05_ 06.cpp // Cube a variable using pass-by-value #include Outline fig 05_ 06.cpp (1 of 2) using std::cout; using std::endl; int cubeByValue( int ); // prototype int main() { int number = 5; Pass number cout . reserved. 1 Chapter 5 - Pointers and Strings Outline 5. 1 Introduction 5. 2 Pointer Variable Declarations and Initialization 5. 3 Pointer Operators 5. 4 Calling Functions by Reference 5. 5 Using const with Pointers 5. 6. Pointers 5. 6 Bubble Sort Using Pass-by-Reference 5. 7 Pointer Expressions and Pointer Arithmetic 5. 8 Relationship Between Pointers and Arrays 5. 9 Arrays of Pointers 5. 10 Case Study: Card Shuffling and. Simulation 5. 11 Function Pointers 5. 12 Introduction to Character and String Processing 5. 12.1 Fundamentals of Characters and Strings 5. 12.2 String Manipulation Functions of the String- Handling