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:24] – [Calling functions by reference] 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. " | ||
- | |||
- | <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 89: | Line 84: | ||
void cubeByReference(int *nPtr) | void cubeByReference(int *nPtr) | ||
{ | { | ||
- | *nPtr = *nPtr * *nPtr * *nPtr; | + | *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability |
} | } | ||
</ | </ | ||
Line 99: | Line 94: | ||
===== 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 140: | 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 149: | 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 161: | Line 167: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
- | |||
- | |||
- | |||
cplusplus/pointers_2.txt · Last modified: 2019/03/28 16:55 by mithat