cplusplus:pointers_2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:pointers_2 [2014/04/22 04:22] – [Example] mithat | cplusplus:pointers_2 [2017/03/21 23:53] – [Pointers as function parameters] mithat | ||
---|---|---|---|
Line 3: | Line 3: | ||
Pointers and comparisons, | Pointers and comparisons, | ||
Deitel, Harvey M., and Paul J. Deitel. " | Deitel, Harvey M., and Paul J. Deitel. " | ||
- | |||
===== Comparing pointers ===== | ===== Comparing pointers ===== | ||
Line 14: | Line 13: | ||
double *bPtr = &y; | double *bPtr = &y; | ||
- | if (aPtr == bPtr) // compare addresses | + | if (aPtr == bPtr) ... // compare addresses |
- | if (*aPtr == *bPtr) // compare contents of addresses, i.e. same as | + | if (*aPtr == *bPtr) |
- | if (x == y) | + | if (x == y) ... |
</ | </ | ||
Line 24: | Line 23: | ||
<file cpp function-with-pointer.cpp> | <file cpp function-with-pointer.cpp> | ||
- | /** Demostrates | + | /** Demonstrates |
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 41: | Line 40: | ||
} | } | ||
- | /** Simple function showing how to pass pointers to functions. */ | + | /** |
+ | * Simple function showing how to pass pointers to functions. | ||
+ | * Print the value of nPtr and the value of dereferenced nPtr. | ||
+ | */ | ||
void printPtr(int *nPtr) | void printPtr(int *nPtr) | ||
{ | { | ||
Line 82: | Line 84: | ||
void cubeByReference(int *nPtr) | void cubeByReference(int *nPtr) | ||
{ | { | ||
- | *nPtr = *nPtr * *nPtr * *nPtr; | + | *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability |
} | } | ||
</ | </ | ||
+ | |||
+ | <WRAP center round tip 90%> | ||
+ | I (and many other programmers) feel this is a much better way of implementing call by reference because the syntax in the function invocation (e.g., '' | ||
+ | </ | ||
===== The const qualifier and pointers ===== | ===== The const qualifier and pointers ===== | ||
- | You are already familiar with C++'s '' | + | You are already familiar with C++'s '' |
* '' | * '' | ||
- | * '' | + | * '' |
* Attempting to change a '' | * Attempting to change a '' | ||
Line 100: | Line 106: | ||
==== Constant pointers ==== | ==== Constant pointers ==== | ||
- | A **constant pointer** is a pointer whose value cannot change; that is, constant pointers store a memory location that cannot be changed. Constant pointers must be initialized when declared, and once declared and initialized, | + | A **constant pointer** is a pointer whose value cannot change; that is, constant pointers store a memory location that cannot be changed. Constant pointers must be initialized when declared, and once initialized |
To declare a constant pointer, use the '' | To declare a constant pointer, use the '' | ||
- | <code cpp> | + | <file cpp const-ptr-demo.cpp> |
- | int x = 2, y = 5; | + | /** Attempting to modify a constant pointer to non-constant data. */ |
- | int *const myPtr = & | + | #include < |
+ | using namespace std; | ||
+ | int main() | ||
+ | { | ||
+ | | ||
+ | int * const myPtr = & | ||
+ | // 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 = & | + | myPtr = & |
- | </code> | + | |
+ | cout << *myPtr << endl; | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </file> | ||
- | ==== Pointer to constant ==== | + | ==== Pointer to constant |
- | You can also create a pointer that is itself not constant (i.e., it can be reassigned or can change "what is being pointed to") but that cannot change the value of "what is being pointed to." This is a **pointer to constant** or **pointer to constant | + | You can also create a pointer that is itself not constant (i.e., it can be reassigned or can change "what is being pointed to") but that cannot change the value of "what is being pointed to." This is a **pointer to constant |
- | To create a pointer to constant, use the '' | + | To create a pointer to constant |
<code cpp> | <code cpp> | ||
Line 126: | Line 145: | ||
</ | </ | ||
- | ==== Constant pointer to constant ==== | + | |
+ | <WRAP center round tip 90%> | ||
+ | One way to remember this is //"'' | ||
+ | |||
+ | In the case of: | ||
+ | <code c++>int *const myPtr = & | ||
+ | '' | ||
+ | <code c++> | ||
+ | '' | ||
+ | </ | ||
+ | |||
+ | ==== Constant pointer to constant | ||
Combining the above, you can create a pointer that can change neither "what it is pointing to" nor the value of "what it is pointing to." | Combining the above, you can create a pointer that can change neither "what it is pointing to" nor the value of "what it is pointing to." | ||
Line 137: | Line 167: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
- | |||
- | ==== Example ==== | ||
- | |||
- | <file cpp const-ptr-demo.cpp> | ||
- | /** Attempting to modify a constant pointer to non-constant data. */ | ||
- | #include < | ||
- | using namespace std; | ||
- | int main() | ||
- | { | ||
- | int x, y; | ||
- | int * const ptr = & | ||
- | // integer. The integer can be modified | ||
- | // through ptr, but ptr always points | ||
- | // to the same memory location. | ||
- | |||
- | *ptr = 7; | ||
- | ptr = & | ||
- | |||
- | return 0; | ||
- | } | ||
- | </ | ||
- | |||
cplusplus/pointers_2.txt · Last modified: 2019/03/28 16:55 by mithat