1 / 23

Chapter 08

Chapter 08 . Pointers and Pointer-Based Strings (Part I). OBJECTIVES. In this part you will learn: What pointers are. The similarities and differences between pointers and references and when to use each. To use pointers to pass arguments to functions by reference. 8.1 Introduction.

tender
Télécharger la présentation

Chapter 08

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 08 Pointers and Pointer-Based Strings (Part I)

  2. OBJECTIVES In this part you will learn: • What pointers are. • The similarities and differences between pointers and references and when to use each. • To use pointers to pass arguments to functions by reference.

  3. 8.1 Introduction • Pointers • Powerful, but difficult to master • Can be used to perform pass-by-reference • Can be used to create and manipulate dynamic data structures • Close relationship with arrays and strings • Example: pointer-based strings char *

  4. 8.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) address value a (34) 45 b (28) a (34) 34 45 Pointer variable

  5. 8.2 Pointer Variable Declarations and Initialization • Pointer declarations • * indicates variable is a pointer • Example • int *myPtr; • Declares pointer to int, of type int * • Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; int * myPtr; myPtr (28) a (34) 34 45

  6. eXERCISE • Examples char *str; double *dbValue; str 123 123 ‘a’ dbValue 54 54 0.001

  7. Pointer initialization • Initialized to 0, NULL, or an address • 0 or NULL points to nothing (null pointer) • Example1: int *myPtr = 0; • Example 2: int *myPtr = NULL;

  8. 8.3 Pointer Operators • Address operator (&) • Returns memory address of its operand • Example • int y = 5;int *yPtr;yPtr = &y;assigns the address of variable y to pointer variable yPtr • Variable yPtr“points to”y • yPtr indirectly references variable y’s value

  9. 8.3 Pointer Operators • Example int y = 5; int *yPtr = &y; cout << y << endl; cout << yPtr << endl; y (14) yPtr 14 5

  10. Error-Prevention Tip 8.1 • Initialize pointers to prevent pointing to unknown or uninitialized areas of memory.

  11. Question: How to get the value of y from its address?

  12. 8.3 Pointer Operators • * operator • Also called indirection operator or dereferencing operator • *yPtr returns y (because yPtr points to y) • Dereferenced pointer is an lvalue *yptr = 9; (equivalent to y = 9;)

  13. Example • int y = 5; • int *yPtr = &y; • cout << y << endl; • cout << yPtr << endl; • *yPtr = 9; • cout << y << endl; • cout << yPtr << endl; • y = 12; • cout << *yPtr << endl; • cout << y << endl; y (14) yPtr 14 5 y (14) yPtr y (14) yPtr

  14. 8.3 Pointer Operators • * and & are inverses of each other • Will “cancel one another out” when applied consecutively in either order • Example: • int y = 5; • int *yPtr = &y; • cout << y << endl; • cout << *(yPtr) << endl; • cout << *(&y) << endl;

  15. Common Programming Error 8.2 • Dereferencing a pointer that has not been properly initialized or that has not been assigned to point to a specific location in memory could cause a fatal execution-time error. • int y = 5; • int *yPtr; • cout << y << endl; • cout << *(yPtr) << endl; Error, because yPtr is not properly initialized.

  16. Common Programming Error • An attempt to dereference a variable that is not a pointer is a compilation error. • Dereferencing a null pointer is normally a fatal execution-time error. • int y = 5; • cout << *y << endl; Error, because y is not a pointer variable. • int y = 5; • int *yPtr = NULL; • cout << y << endl; • cout << *(yPtr) << endl; Error, because NULL denotes noting, which is not a valid address.

  17. 8.4 Passing Arguments to Functions by Reference with Pointers • Three ways to pass arguments to a function • Pass-by-value • Pass-by-reference with reference arguments • Pass-by-reference with pointer arguments • A function can return only one value • Arguments passed to a function using reference arguments • Function can modify original values of arguments • More than one value “returned”

  18. Pass-by-Value • Example #include <iostream> void swap(int x, int y); int main() { int a, b; cin >> a >> b; swap(a, b); cout << a << “ “ << b; return 0; } void swap(int x, in y) { int temp; cout << x << “ “ << y; temp = x; x = y; y = temp; cout << x << “ “ << y; } a b 4 3 temp 4 x y 4 3 3 4

  19. 8.4 Passing Arguments to Functions by Reference with Pointers • Pass-by-reference with pointer arguments • Simulates pass-by-reference • Use pointers and indirection operator. • Pass address of argument using & operator • Arrays not passed with & because array name is already a pointer. • * operator used as alias/nickname for variable inside of function.

  20. Passing Arguments to Functions by Reference with Pointers • Example #include <iostream> void swap(int *x, int *y); int main() { int a, b; cin >> a >> b; swap(&a, &b); cout << a << “ “ << b; return 0; } void swap(int *x, in *y) { int temp; cout << *x << “ “ << *y; temp = *x; *x = *y; *y = temp; cout << *x << “ “ << *y; } a (89) b (100) 3 4 4 3 temp 3 x y 89 100

  21. Pass-by-Reference • Example #include <iostream> void swap(int &x, int &y); int main() { int a, b; cin >> a >> b; swap(a, b); cout << a << “ “ << b; return 0; } void swap(int &x, in &y) { int temp; cout << x << “ “ << y; temp = x; x = y; y = temp; cout << x << “ “ << y; }

  22. Software Engineering Observation 8.1 • Use pass-by-value to pass arguments to a function unless the caller explicitly requires that the called function directly modify the value of the argument variable in the caller. • This is another example of the principle of least privilege.

More Related