cplusplus:pointers_1_slides
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:pointers_1_slides [2019/03/28 15:52] – mithat | cplusplus:pointers_1_slides [2021/10/19 19:29] (current) – [Example] 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. " | ||
Mithat Konar\\ | Mithat Konar\\ | ||
- | March 28, 2019 | + | October 19, 2021 |
===== Introduction ===== | ===== Introduction ===== | ||
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 ===== | ||
- | A //pointer// variable set to point to '' | + | |
+ | * A //pointer// variable set to point to '' | ||
^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ||
- | |'' | + | |'' |
|::: | |::: | ||
|::: | |::: | ||
|::: | |::: | ||
- | The //value// of the pointer variable is the base address | + | * The //value// of a pointer variable is the base address |
===== Pointer visual model ===== | ===== Pointer visual model ===== | ||
Line 60: | Line 61: | ||
===== Pointer syntax basics ===== | ===== Pointer syntax basics ===== | ||
- | ===== Declaration | + | ===== Pointer 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 77: | Line 78: | ||
</ | </ | ||
- | ===== Declaration | + | ===== Pointer declaration |
* You can declare more than one pointer variable at a time: | * You can declare more than one pointer variable at a time: | ||
Line 89: | Line 90: | ||
===== 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 and address operator | + | ===== Pointer 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 108: | ||
myPtr = & | myPtr = & | ||
</ | </ | ||
- | |||
- | The result of this code fragment may be diagrammed as follows: | ||
{{: | {{: | ||
- | and when run may generate | + | ===== Pointer 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 | + | |
+ | ===== Pointer assignment ===== | ||
+ | |||
+ | * Changing | ||
<code cpp> | <code cpp> | ||
Line 139: | Line 143: | ||
</ | </ | ||
- | === Initialization | + | ===== Pinter 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 153: | ||
</ | </ | ||
- | ==== 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 163: | ||
yourPtr = & | yourPtr = & | ||
- | Note how '' | + | * '' |
- | '' | + | ===== Operators for pointers |
- | ==== 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 191: | ||
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 205: | ||
*&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 < | ||
Line 213: | Line 221: | ||
<< | << | ||
<< | << | ||
- | |||
cout << endl; | cout << endl; | ||
Line 224: | Line 231: | ||
<< | << | ||
<< | << | ||
+ | |||
return 0; | return 0; | ||
} | } | ||
</ | </ | ||
cplusplus/pointers_1_slides.txt · Last modified: 2021/10/19 19:29 by mithat