cplusplus:pointers_2_slides
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_slides [2019/03/28 17:36] – mithat | cplusplus:pointers_2_slides [2021/10/19 19:36] – [Calling functions by reference] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
~~NOTOC~~ | ~~NOTOC~~ | ||
- | ====== Pointers 2 ====== | + | ====== Pointers 2 slides |
Pointers and comparisons, | Pointers and comparisons, | ||
Deitel, Harvey M., and Paul J. Deitel. " | Deitel, Harvey M., and Paul J. Deitel. " | ||
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). | ||
+ | ===== Pointers as function parameters ===== | ||
<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. |
- | | + | |
* The syntax in the function invocation (e.g., '' | * The syntax in the function invocation (e.g., '' | ||
+ | ===== Calling functions by reference ===== | ||
<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 88: | Line 89: | ||
</ | </ | ||
- | --------------------------------------------------------------------------------------------------------------- | + | ===== The const qualifier ===== |
- | ===== The const qualifier and pointers ===== | + | |
- | + | * '' | |
- | | + | |
- | * '' | + | |
* '' | * '' | ||
- | | + | |
+ | ===== The const qualifier ===== | ||
+ | | ||
* What is constant? The pointer value or the value of the thing pointed to? | * What is constant? The pointer value or the value of the thing pointed to? | ||
* Answer is //either or both//. | * Answer is //either or both//. | ||
Line 107: | Line 108: | ||
<code c++> | <code c++> | ||
- | int * const myPtr = & | + | int *const myPtr = & |
</ | </ | ||
Line 119: | Line 120: | ||
{ | { | ||
int x = 2, y = 5; | int x = 2, y = 5; | ||
- | int * const myPtr = & | + | int *const myPtr = & |
// The data pointed to by myPtr can be | // The data pointed to by myPtr can be | ||
// modified through myPtr, but myPtr must | // modified through myPtr, but myPtr must | ||
Line 135: | Line 136: | ||
===== Pointer to constant data ===== | ===== Pointer to constant data ===== | ||
- | * 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 '' | + | * Use the '' |
<code cpp> | <code cpp> | ||
Line 164: | Line 165: | ||
<code c++> | <code c++> | ||
- | /* const modifies myPtr'' | + | /* const modifies myPtr, meaning the value of myPtr is constant. */ |
int *const myPtr = &x; | int *const myPtr = &x; | ||
</ | </ | ||
Line 170: | Line 171: | ||
<code c++> | <code c++> | ||
- | /* const'' | + | /* const modifies int, meaning the value of the int is constant. */ |
const int *myPtr = &x; | const int *myPtr = &x; | ||
</ | </ | ||
cplusplus/pointers_2_slides.txt · Last modified: 2021/10/19 19:46 by mithat