Pointers and Mapping 2D Arrays to 1D Arrays
Today we talked about pointers, a part of C (and thus C++) that the community has been moving away from using as much as possible. That said, a lot of code uses pointers (and thus arrays) out there, and your current homework assignment is one of them. So it's a good motivator for learning them.
Essentially, a pointer just holds a memory address of an array or variable (which is sorta like a very small array). If you are holding an array, you will also need to track how many elements are in the array so you don't run off the edge on the back side.
There are two new operators in C++ we learned today:
& - the reference operator, which gives you the memory address of a variable
* - the dereference operator, which gives you the variable at a memory address
They are opposites, so doing *& cancels each other out.
The other big topic today was how 2D coordinates can map to array addresses in a 1D array. In other words, if you have a 10x10 array that is being held in a single int arr[100], you need to be able to convert from a row and column coordinate system to a 1D coordinate system. The algebra to do this is as follows:
row * row_size (i.e. the number of columns) + col
Other Videos By Bill Kerney
2021-10-12 | Color, Story, Level Design |
2021-10-12 | Debugging; Pitch Documents; Time; Timelines to Make a Door |
2021-10-11 | Week 10 Day 1 - Classes Part I |
2021-10-11 | Fallacies Part III |
2021-10-08 | New/Delete, Stack vs. Heap, Old C++ vs. Modern C++ |
2021-10-08 | Fallacies of Composition/Division/Circular Logic |
2021-10-07 | Adding Skeletal Animations to UE4 |
2021-10-07 | Dijkstra Part II + Minimal Spanning Trees |
2021-10-07 | Making a 3D Engine in a Terminal |
2021-10-06 | Fallacies |
2021-10-06 | Pointers and Mapping 2D Arrays to 1D Arrays |
2021-10-06 | Graph Traversal + Dijkstra's Algorithm |
2021-10-06 | The Infamous Kitty Rocket Launcher |
2021-10-06 | Swimming, Delta Time, Terminator Barrel, UMG HUDs |
2021-10-04 | ACM Talk: Hazelton Teaches Vim |
2021-10-04 | Writing to Files, Error Messages in C++ (scary), and Recursion |
2021-10-04 | Midterm Review + Equivocation Fallacy |
2021-10-02 | Reading from Files in C++ |
2021-10-02 | Fallacies Part I |
2021-09-30 | How to export and import assets between projects in UE4 |
2021-09-30 | Making an AI Enemy Part II |