Pointers and comparisons, functions, and qualifiers.1)
Mithat Konar
March 28, 2019
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) ...
/** 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; }
*
operator is used to alias a variable inside of function. cubeByReference(&number))
makes clear that a pointer is involved — good indication that call by reference is used in the function./** 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'' is an alias for the variable passed in. *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability }
const
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
with pointers is more complicated:const
keyword goes between the *
and the name of the pointer:int *const myPtr = &x; // constant pointer to a (non-constant) int // myPtr will always point to x
/** 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; }
const
keyword qualifies 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
int x = 2, y = 5; const int *const myPtr = &x;// constant pointer to a constant int *myPtr = 99; // syntax error! myPtr = &y; // syntax error!
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;