cplusplus:pointers_3_slides
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
cplusplus:pointers_3_slides [2019/03/31 21:05] – created mithat | cplusplus:pointers_3_slides [2021/10/24 02:46] (current) – [Pointer arithmetic: addition] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~SLIDESHOW~~ | ||
+ | ~~NOTOC~~ | ||
+ | |||
====== Pointers 3 slides ====== | ====== Pointers 3 slides ====== | ||
Pointers, arrays, arithmetic.\\ | Pointers, arrays, arithmetic.\\ | ||
Mithat Konar\\ | Mithat Konar\\ | ||
- | March 31, 2019 | + | October 23, 2021 |
===== Pointers and arrays ===== | ===== Pointers and arrays ===== | ||
- | Arrays and pointers | + | * Arrays and pointers are closely related. |
+ | * Array names are like a constant | ||
+ | * the block of memory where an array name points cannot be changed, | ||
+ | * what is stored there can change. | ||
+ | |||
+ | ===== Pointers and arrays ===== | ||
The relationship is so close that you can use pointer operators with array names: | The relationship is so close that you can use pointer operators with array names: | ||
Line 19: | Line 27: | ||
</ | </ | ||
- | And you can use array subscripting operations with pointers: | + | ===== Pointers and arrays ===== |
+ | |||
+ | You can use array subscripting operations with pointers: | ||
<code cpp> | <code cpp> | ||
Line 28: | Line 38: | ||
cout << myPtr[2] << endl; // prints 6 | cout << myPtr[2] << endl; // prints 6 | ||
</ | </ | ||
+ | |||
+ | ===== Pointers and arrays ===== | ||
The following are equivalent: | The following are equivalent: | ||
Line 39: | Line 51: | ||
===== Pointer arithmetic ===== | ===== Pointer arithmetic ===== | ||
- | C++ lets you perform arithmetic on pointer variables; however, // | + | * Pointer |
+ | * '' | ||
+ | * E.g., adding 1 to a pointer makes it point to the next block of memory corresponding to the size of the underlying type. | ||
- | In other words, adding 1 to a pointer makes it point to the next block of memory corresponding to the size of the underlying type; subtracting 1 from a pointer makes it point to the previous block of memory corresponding to the size of the underlying type. This is really only meaningful when performed on an array. | ||
<file cpp pointer-math.cpp> | <file cpp pointer-math.cpp> | ||
Line 65: | Line 78: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== Pointer arithmetic: pointer difference ===== | ||
Subtracting one pointer from another returns the number of elements between two addresses: | Subtracting one pointer from another returns the number of elements between two addresses: | ||
Line 85: | Line 100: | ||
</ | </ | ||
- | ==== Walking down an array ==== | + | ===== Walking down an array ===== |
A common technique used to visit every element in an array is to //walk down an array// with a pointer: | A common technique used to visit every element in an array is to //walk down an array// with a pointer: | ||
Line 109: | Line 124: | ||
</ | </ | ||
- | This is especially | + | Especially |
<file cpp walk-null-terminated.cpp> | <file cpp walk-null-terminated.cpp> | ||
Line 133: | Line 148: | ||
===== Summary ===== | ===== Summary ===== | ||
- | The examples below assume: | + | The following |
<code cpp> | <code cpp> | ||
Line 140: | Line 155: | ||
</ | </ | ||
- | ==== Array access techniques ==== | + | ===== Array access techniques |
^ Access technique | ^ Access technique | ||
Line 149: | Line 164: | ||
- | ==== Array arithmetic ==== | + | ===== Array arithmetic |
^ Operation | ^ Operation | ||
- | |Increment | + | |< |
- | |Decrement | + | |< |
- | |Arithmetic | + | |< |
- | |Compound assignment | + | |< |
valptr += 2; // points at 11</ | valptr += 2; // points at 11</ | ||
- | |Pointer subtraction | + | |< |
cplusplus/pointers_3_slides.1554066338.txt.gz · Last modified: 2019/03/31 21:05 by mithat