9: Pointer Arithmetic. This Is Easy!
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
Now that you know how a pointer can be used to separate groups of items, I’m going to explain another use of pointers that helps you work with the individual items in the group through a special concept called pointer arithmetic. You can add or subtract values from your pointer and it will advance or move backwards the appropriate amount based on how many bytes the item type requires. Most computer languages define an int to occupy 4 bytes of memory. If you have an int pointer and its value is 100, that means there should be an integer at the 4 bytes of memory with addresses 100, 101, 102, and 103. You need all 4 of these bytes to make up that single integer. If you now add 1 to your int pointer, its value doesn’t go from 100 to 101, but it goes from 100 to 104. That’s because the next integer must begin at the next memory address which is 104. With an int pointer, you’re counting by fours. This pattern repeats in both directions. That means if you have an int pointer that’s currently pointing to address 100 and you add 3, then it will change to point to address 112. And if you have an int pointer currently pointing to address 100 and you subtract 5, then it will change to point to address 80. This table shows how a couple integers could be found in memory. The table shows two columns but that’s just for labelling purposes. Computer memory does’t really have columns. It’s just a series of values where each value has an address. So think of the column labelled “Memory Value” as containing the actual values stored in memory while the other column is there just so you can see what the address of each value would be. Memory Address Memory Value 99 Some byte value 100 Part of int #1 101 Part of int #1 102 Part of int #1 103 Part of int #1 104 Part of int #2 105 Part of int #2 106 Part of int #2 107 Part of int #2 108 Some other byte value Listen to the full episode or read the full transcript below for more about pointer arithmetic. Transcript Don’t worry, in case you haven’t figured it out by now, I’ll let you in on a secret. Programmers don’t need a lot of math. Sure, you need a lot of math to get a computer science degree from a university but that’s just because science degrees in general need a lot of math. If you mastered the ability to count by twos, or by threes, or by some other number in grade school, then you’ll have no problem with pointer arithmetic. If you do happen to be good at math, then don’t get me wrong, there are lots of programming jobs that will be able to make use of that. I’m talking about math as it relates to the majority of programmers. Okay, back to pointers. Let’s say that you get an order for 10 super widgets but each one needs to be sent to a different mailing address. You use your index card with its pointer to find out on which shelf the super widgets currently reside and bring back 10 of them. Then you line the super widget boxes up one after the other in your address labeling machine. Your company just invested a lot of money in the latest labeling machine and the salesperson couldn’t stop talking about how this new model just needs you to point to the first box and it does the rest. Well, the salesperson simplified things a bit because when you first tried using the new machine last week, it printed all the labels on the first box and kept going until it jammed. It took you a whole day to clean out the sticky mess. The salesperson was right about pointing to the first box, but you discovered that you need to provide two more pieces of information before you finally got the hang of this. It seems obvious now that that the first piece of missing information is how many boxes to label. I mean a pointer to the first box isn’t enough to know if there’s 1, 2, or 12 boxes total. But a pointer to just the first box is enough to get things going.