cplusplus:pointers_1_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_1_slides [2019/03/28 15:52] – mithat | cplusplus:pointers_1_slides [2021/10/19 19:12] – [Pointer hardware model] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
~~NOTOC~~ | ~~NOTOC~~ | ||
- | ====== Pointers 1 ====== | + | ====== Pointers 1 slides |
Pointer fundamentals, | Pointer fundamentals, | ||
Deitel, Harvey M., and Paul J. Deitel. " | Deitel, Harvey M., and Paul J. Deitel. " | ||
Line 23: | Line 23: | ||
===== Pointer hardware model ===== | ===== Pointer hardware model ===== | ||
- | Assume an '' | + | * Assume an '' |
^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^ | ^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^ | ||
Line 31: | Line 31: | ||
|::: | |::: | ||
- | The address of '' | + | * The address of '' |
===== Pointer hardware model ===== | ===== Pointer hardware model ===== | ||
Line 61: | Line 61: | ||
===== Declaration ===== | ===== Declaration ===== | ||
- | * The '' | + | * '' |
- | * The type of data being pointed to must also be indicated. | + | * Type of data being pointed to must also be indicated. |
<code cpp> | <code cpp> | ||
Line 89: | Line 89: | ||
===== Address operator ===== | ===== Address operator ===== | ||
* To set the value of a pointer, you need the address of something. | * To set the value of a pointer, you need the address of something. | ||
- | * **address operator** ''&'' | + | * **address operator** ''&'' |
- | <code cpp>int num = -42; | + | <code cpp> |
+ | int num = 42; | ||
cout << num << endl; // prints value held in variable num | cout << num << endl; // prints value held in variable num | ||
cout << &num << endl ; // prints the base address of variable num | cout << &num << endl ; // prints the base address of variable num | ||
</ | </ | ||
- | Most environments show base addresses as [[http:// | + | * Most environments show base addresses as [[http:// |
- | ===== Assignment | + | ===== Assignment ===== |
- | * Declare an integer variable '' | + | |
<code cpp> | <code cpp> | ||
int y = 5; // declare an integer variable y | int y = 5; // declare an integer variable y | ||
Line 106: | Line 107: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
- | |||
- | The result of this code fragment may be diagrammed as follows: | ||
{{: | {{: | ||
- | and when run may generate | + | ===== Assignment ===== |
+ | |||
+ | * Declare an integer variable '' | ||
+ | <code cpp> | ||
+ | int y = 5; // declare an integer variable y | ||
+ | int *myPtr; | ||
+ | myPtr = & | ||
+ | </ | ||
^ Variable name ^ Memory location ^ Memory contents | ^ Variable name ^ Memory location ^ Memory contents | ||
|'' | |'' | ||
- | |:::|52001|::: | + | |:::|...|:::| |
- | |:::|52002|:::| | + | |
|::: | |::: | ||
|...|...|...| | |...|...|...| | ||
|'' | |'' | ||
- | |:::|63003|::: | + | |:::|...|:::| |
- | |::: | + | |
- | |::: | + | |
- | |::: | + | |
- | |::: | + | |
- | |:::|63008|:::| | + | |
|::: | |::: | ||
- | You can change | + | |
+ | ===== Assignment ===== | ||
+ | |||
+ | * Chaning | ||
<code cpp> | <code cpp> | ||
Line 139: | Line 142: | ||
</ | </ | ||
- | === Initialization === | + | ===== Initialization |
- | Pointer | + | * Local pointer |
- | <code cpp> | + | * Uninitialized pointers point to arbitrary memory. |
- | leaves the pointer pointing | + | * You can initialize when declared: |
- | Pointer variables can be initialized when declared. It is good programming practice to always initialize pointers so they do not accidentally point to unknown memory locations. The code below initializes the value of the pointer variable in the declaration: | ||
<code cpp> | <code cpp> | ||
int y = 5; | int y = 5; | ||
Line 150: | Line 152: | ||
</ | </ | ||
- | ==== nullptr/ | + | ===== nullptr/ |
- | You can set a pointer to a special | + | * You can set a pointer to a value that indicates that it is //pointing to nothing//: '' |
+ | * Otherwise uninitialized pointers should be set to '' | ||
- | If the memory location that a pointer variable will point to isn’t known at the time it is declared, then you should initialize it to '' | ||
- | |||
- | Below is an example of initializing a pointer variable to '' | ||
<code cpp> | <code cpp> | ||
int *yourPtr = nullptr; // yourPtr points to nothing | int *yourPtr = nullptr; // yourPtr points to nothing | ||
Line 162: | Line 162: | ||
yourPtr = & | yourPtr = & | ||
- | Note how '' | + | * '' |
- | '' | + | ===== Pointer operators ===== |
- | ==== Pointer operators ==== | + | * **address operator** ''&'' |
+ | * **indirection** or **dereferencing operator** '' | ||
- | You were introduced to the **address | + | ===== Indirection/ |
+ | |||
+ | | ||
- | === Indirection/ | ||
- | The **indirection** or **dereferencing** operator, '' | ||
<code cpp> | <code cpp> | ||
int y = -1; // declare y and initialize its value | int y = -1; // declare y and initialize its value | ||
int *myPtr = & | int *myPtr = & | ||
- | cout << *myPtr; | + | cout << *myPtr; |
+ | |||
+ | ===== Indirection/ | ||
+ | |||
+ | * Dereferencing can be used to assign a value to a location in memory: | ||
- | The '' | ||
<code cpp> | <code cpp> | ||
int y = -1; // declare y and initialize its value | int y = -1; // declare y and initialize its value | ||
Line 186: | Line 190: | ||
cout << y; // prints 7</ | cout << y; // prints 7</ | ||
- | You can think of the indirection/ | + | * You can think of the indirection/ |
< | < | ||
- | The '' | + | ===== Indirection/ |
+ | |||
+ | * '' | ||
+ | * The following expressions all evaluate as true: | ||
<code cpp> | <code cpp> | ||
*&y == y | *&y == y | ||
Line 196: | Line 204: | ||
*&myPtr == myPtr</ | *&myPtr == myPtr</ | ||
- | ==== Example ==== | + | ===== Example |
- | The example below demonstrates the use pointers, including address and dereferencing operators. | ||
<file cpp pointer-example.cpp>/ | <file cpp pointer-example.cpp>/ | ||
#include < | #include < |
cplusplus/pointers_1_slides.txt · Last modified: 2021/10/19 19:29 by mithat