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:29] – mithat | cplusplus:pointers_1_slides [2019/03/31 21:03] – 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\\ | ||
+ | March 28, 2019 | ||
===== Introduction ===== | ===== Introduction ===== | ||
Line 11: | Line 12: | ||
* Important in C++ because a close relationship between pointers and arrays, C strings, and references. | * Important in C++ because a close relationship between pointers and arrays, C strings, and references. | ||
- | ===== Address of a variable | + | ===== Pointer variables |
* Variables are stored in blocks of computer memory. | * Variables are stored in blocks of computer memory. | ||
- | * The **base address** | + | * **base address**: the memory address |
- | + | ||
- | ===== Pointer variables ===== | + | |
* **pointer variable**: a variable that stores the base address of some other variable. | * **pointer variable**: a variable that stores the base address of some other variable. | ||
* Usefulness will be seen after understanding how to use them. | * Usefulness will be seen after understanding how to use them. | ||
Line 24: | 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 38: | Line 37: | ||
^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ^ Variable name ^ Memory location ^ Memory contents across all 8 bytes (64 bits) ^ | ||
- | |'' | + | |'' |
|::: | |::: | ||
|::: | |::: | ||
Line 52: | Line 51: | ||
{{ : | {{ : | ||
- | * The //value// of '' | + | The //value// of '' |
===== Pointer visual model ===== | ===== Pointer visual model ===== | ||
- | If we change | + | Changing |
{{ : | {{ : | ||
Line 62: | Line 61: | ||
===== Declaration ===== | ===== Declaration ===== | ||
- | In C++, the '' | + | * '' |
+ | * Type of data being pointed | ||
- | <code cpp>int *myPtr; | + | <code cpp> |
+ | int *myPtr; | ||
+ | bool *yourPtr; | ||
+ | </ | ||
+ | |||
+ | * Location of the '' | ||
- | The location of the '' | ||
<code cpp> | <code cpp> | ||
int *myPtr; | int *myPtr; | ||
Line 72: | Line 76: | ||
int * myPtr; | int * myPtr; | ||
</ | </ | ||
- | You can declare more than one pointer variable at a time: | + | |
+ | ===== Declaration ===== | ||
+ | |||
+ | * You can declare more than one pointer variable at a time: | ||
<code cpp>int *myPtr, *anotherOne; | <code cpp>int *myPtr, *anotherOne; | ||
- | But be careful: | + | * But be careful:: |
<code cpp>int* myPtr, anotherOne; | <code cpp>int* myPtr, anotherOne; | ||
- | Pointers can be created to point to any type: | + | ===== Address operator ===== |
- | <code cpp> | + | * To set the value of a pointer, you need the address of something. |
- | char *yourPtr; | + | * **address operator** ''&'' |
- | double | + | |
- | string | + | |
- | </ | + | |
- | === Assignment === | + | <code cpp> |
- | To set the value of a pointer, you need the address of something. C++ has an **address operator**, ''&'', | + | 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:// | ||
- | The example below declares | + | * Most environments show base addresses as [[http:// |
+ | |||
+ | ===== Assignment ===== | ||
+ | |||
+ | * Declare | ||
<code cpp> | <code cpp> | ||
int y = 5; // declare an integer variable y | int y = 5; // declare an integer variable y | ||
Line 101: | 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 134: | 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 145: | 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 157: | 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 181: | 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 191: | 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