cplusplus:pointers_1_slides
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:pointers_1_slides [2019/03/28 14:42] – created mithat | cplusplus:pointers_1_slides [2021/10/19 19:20] – [Assignment] 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\\ | ||
+ | October 19, 2021 | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | The **pointer** is a concept | + | |
- | + | * Important | |
- | ===== Address of a variable ===== | + | |
- | Before we go any further, you will want to remember that each variable in a program is stored in a particular location in computer memory and that these locations are identified internally using memory addresses. Depending on the size of the variable, the location may occupy a block from one to several bytes. The **base address** of a variable is the memory address corresponding to the starting point of this block. | + | |
===== Pointer variables ===== | ===== Pointer variables ===== | ||
- | You can think of a **pointer variable** | + | * Variables are stored in blocks |
- | + | * **base address**: the memory address of the first byte of this block. | |
- | And? | + | * **pointer variable**: a variable that stores the base address of some other variable. |
- | + | * Usefulness will be seen after understanding how to use them. | |
- | For the moment, let's not consider why you would ever want to do such a thing. It turns out there are lots of cool things you can do with pointers, but you'll just have to trust me on this. Right now, let's just try to get our heads around the concept a little better. | + | |
- | There are two ways you can think about pointers. The first way relates pointers to what is going on // | + | ===== Two ways to think about pointers |
+ | * Pointers | ||
+ | * Pointer visual | ||
- | ==== Pointers in relation to computer | + | ===== Pointer |
- | A regular variable occupies a fixed set of bytes in memory. For example, let's assume we want to create | + | * Assume |
^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^ | ^ Variable name ^ Memory location ^ Memory contents across all four bytes (32 bits)^ | ||
- | |'' | + | |'' |
|::: | |::: | ||
|::: | |::: | ||
|::: | |::: | ||
- | If we were now to ask what the address of '' | + | * The address of '' |
- | So, now let's say that (for whatever reason) we wanted to create a variable to store the address where '' | + | ===== Pointer hardware model ===== |
+ | |||
+ | * 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) ^ | ||
- | |'' | + | |'' |
|::: | |::: | ||
- | |:::|64004|::: | + | |:::|...|:::| |
- | |::: | + | |
- | |::: | + | |
- | |::: | + | |
- | |:::|64008|:::| | + | |
|::: | |::: | ||
- | Note that while '' | + | * The //value// of a pointer variable is the base address of another variable. |
- | ==== Pointers in the abstract | + | ===== Pointer visual model ===== |
- | In the abstract, visual, high-level model of pointers, we can think of them as graphically pointing to other memory locations. For example, given the '' | + | |
+ | Given the '' | ||
{{ : | {{ : | ||
- | the '' | + | a pointer variable |
{{ : | {{ : | ||
- | In this diagram, the //value// of the pointer | + | The //value// of '' |
+ | |||
+ | ===== Pointer visual model ===== | ||
- | Just as the value of a normal variable can change, the value of a pointer can change as well. Let’s say in addition to the variable '' | + | Changing |
{{ : | {{ : | ||
- | ==== Pointer syntax basics ==== | + | ===== Pointer syntax basics |
- | === 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 71: | Line 77: | ||
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 100: | Line 108: | ||
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 | + | |
+ | ===== Pointer assignment ===== | ||
+ | |||
+ | * Changing | ||
<code cpp> | <code cpp> | ||
Line 133: | Line 143: | ||
</ | </ | ||
- | === 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 144: | 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 156: | Line 163: | ||
yourPtr = & | yourPtr = & | ||
- | Note how '' | + | * '' |
+ | |||
+ | ===== Pointer operators ===== | ||
- | '' | + | * **address operator** |
+ | * **indirection** or **dereferencing operator** | ||
- | ==== Pointer operators | + | ===== Indirection/ |
- | You were introduced to the **address operator**, ''&'', | + | |
- | === 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 180: | 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 190: | 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 < |
cplusplus/pointers_1_slides.txt · Last modified: 2021/10/19 19:29 by mithat