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 [2016/03/05 22:35] – [Constant pointer to constant data] mithat | ||
---|---|---|---|
Line 45: | Line 45: | ||
} | } | ||
- | /** 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 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 157: | Line 162: | ||
</ | </ | ||
- | + | <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 = & | ||
+ | </ | ||
cplusplus/pointers_2.txt · Last modified: 2019/03/28 16:55 by mithat