cplusplus:pointers_2_slides
This is an old revision of the document!
Pointers 2
Pointers and comparisons, functions, and qualifiers.1)
Mithat Konar
March 28, 2019
Comparing pointers
- Relational operators can be used with pointer variables.
- Be aware of the difference between comparing the value of the pointer (an address) and comparing the contents of that address.
double x = 2.2; double y = 4.8; double *aPtr = &x; double *bPtr = &y; if (aPtr == bPtr) ... // compare addresses if (*aPtr == *bPtr) ... // compare contents of addresses, i.e. same as if (x == y) ...
Pointers as function parameters
- Pointers can be used as function parameters.
- Arguments can be anything that can be assigned to a pointer (i.e., addresses or pointers).
- function-with-pointer.cpp
/** Demonstrates how to pass pointers to functions. */ #include <iostream> using namespace std; void printPtr(int *); // prototype int main() { int number = 5; int *myPtr = &number; printPtr(myPtr); // pass in a pointer printPtr(&number); // pass in an address return 0; } /** * Simple function showing how to pass pointers to functions. * Print the value of nPtr and the value of dereferenced nPtr. */ void printPtr(int *nPtr) { cout << "The value of the pointer is " << nPtr << endl << "The value of the thing pointed to by the pointer is "<< *nPtr << endl; }
Calling functions by reference
- Pointers can be used to implement pass by reference in functions.
- The
*
operator is used to alias a variable inside of function. - In the function below,
*number
is used as an alias for the variable passed in. - Strictly speaking, the passing mechanism is call by value. But functionally it's pass by reference.
- The syntax in the function invocation (e.g.,
cubeByReference(&number))
makes it clear that a pointer is involved — which is good indication that call by reference is being used.
- cube-by-reference.cpp
/** Cube a variable using pass by reference with a pointer. */ #include <iostream> using namespace std; void cubeByReference(int *); // prototype int main() { int number = 5; cout << "The original value of number is " << number << endl; cubeByReference(&number); cout << "The new value of number is " << number << endl; return 0; } /** Cube the integer pointed to by nPtr. */ void cubeByReference(int *nPtr) { *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability }
The const qualifier and pointers
const
qualifier when used with regular variables:const
variables cannot be changed.const
should be used on variables and function parameters when the variable or parameter's value should not change.
const
qualifier with pointers is more complicated:- What is constant? The pointer value or the value of the thing pointed to?
- Answer is either or both.
Constant pointers
- A constant pointer is a pointer whose value cannot change.
- Constant pointers store a memory location that cannot be changed.
- Must be initialized when declared.
- Use the
const
keyword between the*
and the name of the pointer:
int * const myPtr = &x; // constant pointer to a (non-constant) int
Example
- const-ptr-demo.cpp
/** Attempting to modify a constant pointer to non-constant data. */ #include <iostream> using namespace std; int main() { int x = 2, y = 5; int * const myPtr = &x; // constant pointer to a (non-constant) int // The data pointed to by myPtr can be // modified through myPtr, but myPtr must // always point to the same memory location. *myPtr = 99; // change value stored in x to 99 myPtr = &y; // syntax error!! can't point to a different int cout << *myPtr << endl; return 0; }
Pointer to constant data
- A pointer to constant data cannot change the value of what it's pointing to.
- Can point to different things.
- Use the
const
keyword to qualify the type being pointed to.
int x = 2, y = 5; const int *myPtr = &x; // non-constant pointer to a constant int *myPtr = 99; // syntax error! can't modify what it's pointing to myPtr = &y; // no problem, myPtr now points to y
Constant pointer to constant data
- Constant pointer to constant data cannot change either “what it is pointing to” nor the value of “what it is pointing to.”
int x = 2, y = 5; const int *const myPtr = &x;// constant pointer to a constant int *myPtr = 99; // syntax error! myPtr = &y; // syntax error!
How to remember
const
modifies the thing immediately following it.
/* const modifies myPtr, meaning the value of myPtr is constant. */ int *const myPtr = &x;
/* const modifies int, meaning the value of the int is constant. */ const int *myPtr = &x;
1)
Portions loosely adapted from:
Deitel, Harvey M., and Paul J. Deitel. “Pointers and Strings.” In C++: How to Program. 3 ed. Upper Saddle River, NJ: Prentice Hall, 2001. 304-388.
cplusplus/pointers_2_slides.1553794849.txt.gz · Last modified: 2019/03/28 17:40 by mithat