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 [2016/03/05 22:21] – [Pointers as function parameters] mithat | cplusplus:pointers_2 [2019/03/11 19:51] – [Calling functions by reference] 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. | + | /** |
- | * Print the value of nPtr and the value of dereferenced nPtr. | + | * 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 88: | 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 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 '' |
* '' | * '' | ||
Line 135: | 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 144: | Line 144: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
+ | |||
+ | |||
+ | <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 data ==== | ==== Constant pointer to constant data ==== | ||
Line 156: | Line 167: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
- | |||
- | |||
- | |||
cplusplus/pointers_2.txt · Last modified: 2019/03/28 16:55 by mithat