cplusplus:pointers_2_slides
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:pointers_2_slides [2019/04/01 23:13] – mithat | cplusplus:pointers_2_slides [2021/10/19 19:46] (current) – [Constant pointer to constant data] mithat | ||
---|---|---|---|
Line 28: | Line 28: | ||
* Arguments can be anything that can be assigned to a pointer (i.e., addresses or pointers). | * Arguments can be anything that can be assigned to a pointer (i.e., addresses or pointers). | ||
+ | ===== Example ===== | ||
<file cpp function-with-pointer.cpp> | <file cpp function-with-pointer.cpp> | ||
/** Demonstrates how to pass pointers to functions. */ | /** Demonstrates how to pass pointers to functions. */ | ||
Line 62: | Line 63: | ||
* Pointers can be used to implement //pass by reference// in functions. | * Pointers can be used to implement //pass by reference// in functions. | ||
* The '' | * The '' | ||
- | | + | * Strictly speaking, the passing mechanism is //call by value//. But functionality is pass by reference. |
- | | + | * Syntax |
- | * The syntax | + | |
+ | ===== Example ===== | ||
<file cpp cube-by-reference.cpp> | <file cpp cube-by-reference.cpp> | ||
/** Cube a variable using pass by reference with a pointer. */ | /** Cube a variable using pass by reference with a pointer. */ | ||
Line 84: | Line 85: | ||
void cubeByReference(int *nPtr) | void cubeByReference(int *nPtr) | ||
{ | { | ||
+ | // *nptr'' | ||
*nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability | *nPtr = (*nPtr) * (*nPtr) * (*nPtr); // parenthesis for readability | ||
} | } | ||
Line 102: | Line 104: | ||
* A **constant pointer** is a pointer whose value cannot change. | * A **constant pointer** is a pointer whose value cannot change. | ||
- | * Constant pointers store a memory location that cannot be changed. | + | * Constant pointers |
* Must be initialized when declared. | * Must be initialized when declared. | ||
- | * Use the '' | + | * '' |
<code c++> | <code c++> | ||
int *const myPtr = & | int *const myPtr = & | ||
+ | // myPtr will always point to x | ||
</ | </ | ||
Line 137: | Line 140: | ||
* A **pointer to constant data** cannot change the value of what it's pointing to. | * A **pointer to constant data** cannot change the value of what it's pointing to. | ||
* Can point to different things. | * Can point to different things. | ||
- | * Use the '' | + | * '' |
<code cpp> | <code cpp> | ||
Line 149: | Line 152: | ||
===== Constant pointer to constant data ===== | ===== Constant pointer to constant data ===== | ||
- | * **Constant pointer to constant data** cannot change either "what it is pointing to" | + | * **Constant pointer to constant data** cannot change either "what it is pointing to" |
<code cpp> | <code cpp> |
cplusplus/pointers_2_slides.1554160381.txt.gz · Last modified: 2019/04/01 23:13 by mithat