From 1ca9a2b5b4faa7d54a3dcded8d978d29563a8e69 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Mon, 11 Sep 2023 00:29:12 -0400 Subject: [PATCH] adding lecture 4 notes --- lectures/04_pointers/README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lectures/04_pointers/README.md diff --git a/lectures/04_pointers/README.md b/lectures/04_pointers/README.md new file mode 100644 index 0000000..a08745c --- /dev/null +++ b/lectures/04_pointers/README.md @@ -0,0 +1,32 @@ +# Lecture 4 --- Pointers, Arrays, & Pointer Arithmetic + +- Pointers store memory addresses. +- They can be used to access the values stored at their stored memory address. +- They can be incremented, decremented, added, and subtracted. +- Dynamic memory is accessed through pointers. +- Pointers are also the primitive mechanism underlying vector iterators, which we have used with std::sort and +will use more extensively throughout the semester. + +## 4.1 Pointer Example + +- Consider the following code segment: + +```cpp +float x = 15.5; +float *p; /* equiv: float* p; or float * p; */ +p = &x; +*p = 72; +if ( x > 20 ) +cout << "Bigger\n"; +else +cout << "Smaller\n"; +``` + +The output is Bigger +because x == 72.0. What’s going on? + +## 4.2 Exercises + +- [Leetcode problem 905: Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/). Solution: [p905_sortarraybyparity.cpp](../../leetcode/p905_sortarraybyparity.cpp) +- [Leetcode problem 977: Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/). Solution: [p977_sortedsquare.cpp](../../leetcode/p977_sortedsquare.cpp) +- [Leetcode problem 1051: Height Checker](https://leetcode.com/problems/height-checker/). Solution: [p1051_heightchecker.cpp](../../leetcode/p1051_heightchecker.cpp)