Last time, we learned how we can dereference a pointer to change the value of the memory location that the pointer points to. This time, we'll learn three different ways. We can work an array using pointers. The first part of the code I wrote for this lecture doesn't use pointers at all. I am declaring an array of four integers, and I'm initializing that array to hold 190, 90 and 80. I'm also calculating the number of elements in the array by taking the total size of the array, and dividing by the size of a single element in the array. We saw that previously when we worked with arrays. Now, I'm just doing a for-loop and printing out each of the scores. Notice, I'm saying score i plus one so that I'll start at score one because my output is for humans, not programmers. I have an integer pointer that I've called pScores. Remember, you don't have to precede pointer names with a lowercase P, but I'm doing that to distinguish this particular variable from my scores variable. So I have a pointer to integers and I'm setting the address that pScores holds to the address of the first element of the scores right. Now I'm doing a for loop. I print out the element address which is held in pScores. I print out the contents of that memory address by dereferencing the pointer using the indirection or dereference operator. So for each of the elements in the array, I'm printing out the address of that element and the contents of that memory location and then I increment pScores. We'll come back to that increment in a moment, but first, I want to run the code. As you can see, I have the four addresses for the four different elements, and the contents of those addresses are correct based on what's actually in the scores array. The reason I said we'd come back to the increment is because, all of the incrementing we've done up to this point in the course has meant add one. But when we increment a pointer, the compiler is smart enough to say, "I'm going to increment by the size of whatever data type the pointer points to, so that incrementing the pointer actually moves me to the next pointed two entity if you want to think of it that way." So the memory addresses here jumped by four each time. That may seem strange to you but that is eight plus four in hex. So increment for pointers means add to the memory address the size of whatever is pointed to. In this case, an int which is four bytes for my computer and my compiler. I also have some code written down below that does the same thing with floats just to show that increment works that way for floats as well. As you can see, although if you don't know hex you may not realize this. But C plus 8 is 14, and 14 plus 8 is 1C and so on. So the code increments my pointer to doubles. So in this particular case down below, I'm incrementing by eight. I'm increasing my memory location by eight each time I increment my pointer, because the compiler knows I'm pointing to doubles. I'm going to comment this back out again. Now, there is a problem with this for-loop the way I've implemented it. The problem is, when we finish with this for-loop, pScores has been destroyed. It no longer points to the beginning of the scores array. It points four bytes past the end of the scores array. If we were to dereference pScores at that point, we'd take whatever was in that memory location and interpreted as an integer if we dereferenced it, and that would be really real a bad idea because that's not part of the array. Let's look at another way to do this hat doesn't actually destroy pScores. Here, you'll see the address that I'm printing out is actually pScores plus i. The first time through the loop I is zero. The address will be pScores. The second time through the loop, the address will be pScores plus 1. But again, because the compiler knows I'm pointing to an int, pScores plus 1, actually moves us four bytes forward on this computer with this compiler to the next integer. To get at the contents of pScores plus i, I'm still going to dereference a pointer, but the pointer ID reference is pScores plus i. You could certainly argue, well, is that a pointer or is that just a memory address? Really, the answer is it's a memory address, but I can always just dereference a memory address to see what the contents of that memory address are. So I could actually hard code memory addresses in my code here and dereference them and get at what's in those memory addresses. That is very, very uncommon because we don't know what memory addresses the runtime system will give us when we run our code, but this is perfectly valid. So the first time through the loop, I'll dereference pScores and the second time through the loop I'll dereference pScores plus 1 which is the pScores memory address plus four bytes. Let me show you that that works. As you can see where incrementing by four each time and we're getting the correct contents from the memory addresses. One more way that I want to show you we can do this. We can actually act like pScores refers to an array, and index into the array using square brackets just like we typically index into arrays. Notice I'm not doing this. I'm not using scores. I'm using pScores, but I'm acting like it's an array. If I'm going to print out the address, I need to use the address of operator. But when I went to print out the contents or the element, I just act as though pScores is an array, not a pointer to an int. I'll run it again. As you can see, it's all working properly. Here, you can see 0 plus 4 is 4. So the memory addresses are incrementing properly and the contents are being displayed correctly as well. You may have noticed that we're getting different memory addresses each time I run the code. That's because the runtime system just goes and gets the memory that we need, but it doesn't try to give us what it gave us last time we ran the code or anything like that. It just goes and finds available memory for us and gives it to us. Before we leave this code, I want to show you one more thing we can do up here when we're assigning a value to pScores. Instead of saying pScores gets the address of the first element of the array, we can actually say this. We can say pScores gets assigned scores. If I run it again, we can see that everything still works properly. So we can in fact say that this pointer's value, the memory address for this value is actually the array. I tend not to do it that way, I tend to do it this way instead. I do it this way because when I'm assigning a value to a pointer, it's nice for me to see that I'm explicitly assigning an address. So I like to use the address of operator on the right-hand side when I'm putting a value into a pointer. To recap, in this lecture, we learned three different ways to walk an array using pointers