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:27] – [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 14: | Line 18: | ||
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 45: | ||
} | } | ||
- | /** 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 89: | ||
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 ===== | ||
Line 100: | Line 111: | ||
==== 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 '' |
cplusplus/pointers_2.txt · Last modified: 2019/03/28 16:55 by mithat