~~SLIDESHOW~~ ~~NOTOC~~ ====== Pointers 2 slides ====== Pointers and comparisons, functions, and qualifiers.((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.))\\ 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). ===== Example ===== /** Demonstrates how to pass pointers to functions. */ #include 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. * Strictly speaking, the passing mechanism is //call by value//. But functionality is pass by reference. * Syntax in function invocation (e.g., ''cubeByReference(&number))'' makes clear that a pointer is involved --- good indication that call by reference is used in the function. ===== Example ===== /** Cube a variable using pass by reference with a pointer. */ #include 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 } ===== The const qualifier ===== * ''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. ===== The const qualifier ===== * ''const'' 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. * ''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 ===== Example ===== /** Attempting to modify a constant pointer to non-constant data. */ #include 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. * ''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 ===== Constant pointer to constant data ===== * **Constant pointer to constant data** cannot change either "what it is pointing to" or 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;