cplusplus:pointers_2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:pointers_2 [2016/03/05 22:19] – [Comparing pointers] mithat | cplusplus:pointers_2 [2019/03/28 16:55] (current) – 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. " | ||
- | |||
- | <WRAP center round important 60%> | ||
- | This content has not been vetted for C++11 compliance. | ||
- | </ | ||
- | |||
===== Comparing pointers ===== | ===== Comparing pointers ===== | ||
Line 28: | 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 45: | 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 56: | Line 54: | ||
===== Calling functions by reference ===== | ===== Calling functions by reference ===== | ||
- | Pointers can be used to implement //call-by-reference// in functions. The '' | + | Pointers can be used to implement //passing |
<code cpp> | <code cpp> | ||
Line 65: | Line 63: | ||
</ | </ | ||
- | In the function above, '' | + | In the function above, '' |
<file cpp cube-by-reference.cpp> | <file cpp cube-by-reference.cpp> | ||
- | /** Cube a variable using call-by-reference with a pointer. */ | + | /** Cube a variable using pass by reference with a pointer. */ |
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 86: | Line 84: | ||
void cubeByReference(int *nPtr) | void cubeByReference(int *nPtr) | ||
{ | { | ||
- | *nPtr = *nPtr * *nPtr * *nPtr; | + | *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability |
} | } | ||
</ | </ | ||
+ | |||
+ | <WRAP center tip 90%> | ||
+ | I feel pointers are much better than reference parameters for implementing pass 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 '' |
* '' | * '' | ||
Line 133: | Line 135: | ||
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 data** or simply **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 data** or simply **pointer to constant**. | ||
- | To create a pointer to constant, use the '' | + | To create a pointer to constant |
<code cpp> | <code cpp> | ||
Line 142: | Line 144: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
+ | |||
+ | |||
+ | <WRAP center tip 90%> | ||
+ | One way to remember this is //"'' | ||
+ | |||
+ | In the case of: | ||
+ | <code c++>int *const myPtr = & | ||
+ | '' | ||
+ | |||
+ | In the case of: | ||
+ | <code c++> | ||
+ | '' | ||
+ | </ | ||
==== Constant pointer to constant data ==== | ==== Constant pointer to constant data ==== | ||
Line 154: | Line 169: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
- | |||
- | |||
- | |||
cplusplus/pointers_2.1457216371.txt.gz · Last modified: 2016/03/05 22:19 by mithat