chapter 08
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/1.jpg)
Pointers and Pointer-Based Strings (Part I)
![Page 2: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/2.jpg)
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.
![Page 3: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/3.jpg)
Pointers Powerful, but difficult to masterCan be used to perform pass-by-reference Can be used to create and manipulate
dynamic data structuresClose relationship with arrays and strings
Example: pointer-based stringschar *
![Page 4: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/4.jpg)
Pointer variablesContain memory addresses as values
Normally, variable contains specific value (direct reference)
Pointers contain address of variable that has specific value (indirect reference)
a(34) 45
address value
a(34) 45 b
(28) 34
Pointer variable
![Page 5: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/5.jpg)
Pointer declarations* indicates variable is a pointer
Exampleint *myPtr;
Declares pointer to int, of type int *
Multiple pointers require multiple asterisksint *myPtr1, *myPtr2;
a(34) 45 myPtr
(28) 34
int * myPtr;
![Page 6: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/6.jpg)
Exampleschar *str;
double *dbValue;
123 ‘a’ str 123
54 0.001
dbValue
54
![Page 7: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/7.jpg)
Initialized to 0, NULL, or an address0 or NULL points to nothing (null pointer)Example1:
int *myPtr = 0;Example 2:
int *myPtr = NULL;
![Page 8: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/8.jpg)
Address operator (&)Returns memory address of its operandExample
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
![Page 9: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/9.jpg)
Exampleint y = 5;int *yPtr = &y;cout << y << endl;cout << yPtr << endl;
y(14) 5 yPtr
14
![Page 10: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/10.jpg)
Initialize pointers to prevent pointing to unknown or uninitialized areas of memory.
![Page 11: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/11.jpg)
Question: How to get the value of y from its address?
![Page 12: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/12.jpg)
* operatorAlso 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;)
![Page 13: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/13.jpg)
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) 5 yPtr
14
y(14) yPtr
y(14) yPtr
![Page 14: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/14.jpg)
* and & are inverses of each otherWill “cancel one another out” when applied
consecutively in either orderExample:
int y = 5;int *yPtr = &y;cout << y << endl;cout << *(yPtr) << endl;cout << *(&y) << endl;
![Page 15: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/15.jpg)
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.
![Page 16: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/16.jpg)
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;
int *yPtr = NULL;cout << y << endl;cout << *(yPtr) << endl;
Error, because NULL denotes
noting, which is not a valid address.
int y = 5;cout << *y << endl; Error, because y
is not a pointer variable.
![Page 17: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/17.jpg)
1 // Fig. 8.4: fig08_04.cpp 2 // Using the & and * operators. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int a; // a is an integer 10 int *aPtr; // aPtr is an int * -- pointer to an integer 11 12 a = 7; // assigned 7 to a 13 aPtr = &a; // assign the address of a to aPtr
14 15 cout << "The address of a is " << &a 16 << "\nThe value of aPtr is " << aPtr; 17 cout << "\n\nThe value of a is " << a 18 << "\nThe value of *aPtr is " << *aPtr; 19 cout << "\n\nShowing that * and & are inverses of " 20 << "each other.\n&*aPtr = " << &*aPtr 21 << "\n*&aPtr = " << *&aPtr << endl; 22 return 0; // indicates successful termination 23 } // end main The address of a is 0012F580 The value of aPtr is 0012F580 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012F580 *&aPtr = 0012F580
![Page 18: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/18.jpg)
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”
![Page 19: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/19.jpg)
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;}
4 3
temp
x y
4
3 4
a b
4 3
![Page 20: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/20.jpg)
Pass-by-reference with pointer argumentsSimulates 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.
![Page 21: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/21.jpg)
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;}
89 100
temp
x y
3
a (89) b (100)
3 44 3
![Page 22: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/22.jpg)
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;}
![Page 23: Chapter 08](https://reader033.vdocuments.net/reader033/viewer/2022051700/56815e09550346895dcc61f6/html5/thumbnails/23.jpg)
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.