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
Pointers and Pointer-Based Strings (Part I)
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.
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 *
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
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;
Exampleschar *str;
double *dbValue;
123 ‘a’ str 123
54 0.001
dbValue
54
Initialized to 0, NULL, or an address0 or NULL points to nothing (null pointer)Example1:
int *myPtr = 0;Example 2:
int *myPtr = NULL;
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
Exampleint y = 5;int *yPtr = &y;cout << y << endl;cout << yPtr << endl;
y(14) 5 yPtr
14
Initialize pointers to prevent pointing to unknown or uninitialized areas of memory.
Question: How to get the value of y from its address?
* 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;)
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
* 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;
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.
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.
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
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”
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
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.
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
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;}
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.