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:40] – mithat | cplusplus:pointers_2 [2016/03/05 22:38] – [Pointers 2] 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 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 '' |
* '' | * '' | ||
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 initialized, //a constant pointer | + | 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 cannot be reassigned. |
To declare a constant pointer, use the '' | To declare a constant pointer, use the '' | ||
Line 129: | 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 138: | Line 144: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
+ | |||
+ | |||
+ | <WRAP center round tip 90%> | ||
+ | One way to remember this is "const modifies the thing immediately following it." In the case of: | ||
+ | <code c++>int *const myPtr = & | ||
+ | '' | ||
+ | <code c++> | ||
+ | '' | ||
+ | </ | ||
==== Constant pointer to constant data ==== | ==== Constant pointer to constant data ==== | ||
Line 150: | Line 165: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
- | |||
- | |||
- | |||
cplusplus/pointers_2.txt · Last modified: 2019/03/28 16:55 by mithat