10: Double Pointers! The Pattern Continues.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
A double pointer is nothing more than a pointer to another pointer. It’s possible to continue this pattern indefinitely having pointers to pointers to pointers, and so on. But you’ll rarely need more than a double pointer. Previous episodes explained how you can use pointers to either point to a group of items or to individual items within a collection. Now you’ll learn how to use a pointer that points to another pointer. Let’s say we have two items: ItemA and ItemB and they both live somewhere in memory. It doesn’t matter where or in what order they are to each other. Now you have a pointer to each of them called pItemA and pItemB and for these pointers, you do want them positioned in a certain order. If you want ItemA to come before ItemB, then put pItemA in front of pItemB. Pointers are small and can be moved around in memory very easy. Maybe a lot easier than trying to move the actual ItemA and ItemB themselves. Now that you have your two pointers to the items positioned in memory one right next to the other, you can refer to them both with another pointer. We’ll create another pointer called pItemCollection and make it point to whichever of the item pointers is first. In this case, pItemCollection will point to pItemA. So far, you should be able to understand this if you take it one step at a time. It will help if you draw it on paper. Use boxes for each of the items and for each of the pointers and use arrows from each pointer that goes to whatever it points to. The difficult part comes when trying to understand how to call a method that can modify your pItemCollection pointer. You can’t pass the pointer directly to the method because then it will get its own pointer value that also points to pItemA. So what you need to do is provide the method with the address of where your pItemCollection pointer lives in memory so that it can change it directly. You pass the address of something with another pointer. We’ll talk about this again when I explain methods. Listen to the full episode or you can also read the full transcript below. Transcript Why would you ever need a double pointer in the first place? The example in episode 8 seems to solve everything already, right? I mean, you’ve got a bunch of items each sitting at some row and shelf address and by themselves, they are completely unorganized so that they can make the best use of available space. They used to be organized but as new shipments arrived, the items were put on shelves that had room. They were kept together at least but there is no longer any relation from one group of items to the other groups sitting to the left and right. All of this is okay because you use pointers to organize what would otherwise be a mess. Imagine walking into Home Depot and finding light bulbs next to the concrete which is next to a refrigerator. And not even all the refrigerators, just one type. To browse all the refrigerators, you would have to walk back and forth through the whole warehouse. The only reason the previous example worked at all is because there is no need to organize the items for direct customer browsing. The unorganized layout constantly changes and the only thing that brings it under control is the stack of index cards. The stack of index cards is organized for quick and efficient searching. But I still haven’t described double pointers yet. I’m getting to that right now. Let’s say that this system works so well that the inventory grows and so does the stack of index cards. In fact it’s no longer just a stack of index cards. They occupy a large box and this box is getting too big to fit on your desk. You need to find someplace to keep the box of index cards when it’s not being used so why not use one of the empty shelves for this too. You soon discover though that just like the other items get moved around on the shelves sometimes your box of index cards gets in the way and also need