cplusplus:pointers_2_slides
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:pointers_2_slides [2019/03/28 16:48] – created mithat | cplusplus:pointers_2_slides [2021/10/19 19:40] – [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 60: | Line 61: | ||
===== Calling functions by reference ===== | ===== Calling functions by reference ===== | ||
- | * Pointers can be used to implement //call-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 in function invocation (e.g., |
+ | ===== Calling functions by reference ===== | ||
<file cpp cube-by-reference.cpp> | <file cpp cube-by-reference.cpp> | ||
- | /** Cube a variable using call-by-reference with a pointer. */ | + | /** Cube a variable using pass by reference with a pointer. */ |
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 83: | 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 | ||
} | } | ||
</ | </ | ||
- | <WRAP center tip 90%> | + | ===== The const qualifier ===== |
- | I feel pointers are much better than reference parameters for implementing call by reference because the syntax in the function invocation (e.g., '' | + | |
- | </ | + | |
- | ===== The const qualifier | + | * '' |
+ | * '' | ||
+ | * '' | ||
- | You are already familiar with C++' | + | ===== The const qualifier ===== |
+ | * '' | ||
+ | * What is constant? The pointer value or the value of the thing pointed to? | ||
+ | * Answer is //either or both//. | ||
- | * '' | + | ===== Constant pointers ===== |
- | * '' | + | |
- | * Attempting to change a '' | + | |
- | Applying | + | * A **constant pointer** is a pointer whose value cannot change. |
+ | * Constant pointers store a memory location that cannot be changed. | ||
+ | * Must be initialized when declared. | ||
+ | * Use the '' | ||
- | The answer is //either or both//. | + | <code c++> |
- | + | int *const myPtr = & | |
- | ==== Constant pointers ==== | + | </ |
- | + | ||
- | A **constant pointer** is a pointer whose value cannot change; that is, constant | + | |
- | To declare a constant pointer, use the '' | + | ===== Example ===== |
<file cpp const-ptr-demo.cpp> | <file cpp const-ptr-demo.cpp> | ||
Line 116: | Line 121: | ||
{ | { | ||
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 122: | Line 127: | ||
*myPtr = 99; // change value stored in x to 99 | *myPtr = 99; // change value stored in x to 99 | ||
- | myPtr = & | + | myPtr = & |
cout << *myPtr << endl; | cout << *myPtr << endl; | ||
Line 130: | Line 135: | ||
</ | </ | ||
- | ==== Pointer to constant data ==== | + | ===== Pointer to constant data ===== |
- | You can also create a pointer | + | * A **pointer |
- | + | | |
- | To create a pointer to constant data, use the '' | + | * Use the '' |
<code cpp> | <code cpp> | ||
Line 140: | Line 145: | ||
const int *myPtr = & | const int *myPtr = & | ||
- | *myPtr = 99; // syntax error! | + | *myPtr = 99; // syntax error! |
myPtr = & | myPtr = & | ||
</ | </ | ||
+ | ===== Constant pointer to constant data ===== | ||
- | <WRAP center tip 90%> | + | * **Constant pointer to constant data** cannot |
- | One way to remember this is //"'' | + | |
- | + | ||
- | In the case of: | + | |
- | <code c++> | + | |
- | '' | + | |
- | + | ||
- | In the case of: | + | |
- | <code c++> | + | |
- | '' | + | |
- | </ | + | |
- | + | ||
- | ==== Constant pointer to constant data ==== | + | |
- | + | ||
- | Combining the above, you can create a pointer that can change | + | |
<code cpp> | <code cpp> | ||
Line 167: | Line 159: | ||
*myPtr = 99; // syntax error! | *myPtr = 99; // syntax error! | ||
myPtr = & | myPtr = & | ||
+ | </ | ||
+ | |||
+ | ===== How to remember ===== | ||
+ | |||
+ | * '' | ||
+ | |||
+ | <code c++> | ||
+ | /* const modifies myPtr, meaning the value of myPtr is constant. */ | ||
+ | int *const myPtr = &x; | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code c++> | ||
+ | /* const modifies int, meaning the value of the int is constant. */ | ||
+ | const int *myPtr = &x; | ||
</ | </ | ||
cplusplus/pointers_2_slides.txt · Last modified: 2021/10/19 19:46 by mithat