In this lecture, we'll talk about pointer basics. And although we've used pointers off and on throughout the course, we're going to start at the beginning as we work our way through our exploration of pointers. So what is a pointer? A pointer is a variable that holds a memory address. If in fact the pointer's value is null, that means it doesn't actually point to a valid memory address, and null is actually zero. But we know if a pointer is null, that it's not valid. We shouldn't try to do anything with that pointer. Speaking of doing things with pointers, what can we do with pointers? Well, we can point to stuff. And that feels sarcastic because I am sarcastic. But really, what I'm saying is that we can refer to a particular place in memory. Now, referring to a particular place in memory isn't necessarily useful unless we can see what's in side that memory location. So with pointers, we can also de-reference the pointer to see what the value is at that memory location. We can also do math with pointers to actually move along in memory. Usually, we walk from one memory location to the next in contiguous blocks of memory. Blocks of pieces of memory that are next to each other. But we could jump around in memory, but usually people don't do that. So as we explore pointers, we'll see how to do those three things and even more. Let's go look at how we can actually set a value of a pointer and de-reference that pointer to see what's stored at that memory location. I've already written the code that we'll use to explore the basic ideas behind pointers. I've declared a variable called score, whose value I've initialized to 500. Here, I print out the value of the variable. We've been doing that since very early in the course. Here, I print out the address of score, not just score but the actual memory address that's been allocated for the score variable. And we've seen this since early on as well, when we've used scanf to get input from the user. And we've also seen the address of operator periodically when we were doing string processing. And then finally, I print the size of score. So these three pieces of information the value of the variable, the address of the variable, and how many bytes the variable takes up are all things that we've seen before. We've actually seen some of this pointer stuff before as well, but now we're just focusing on the pointer stuff. So I'm going to declare a pointer and display pointer information. I've said that I want to pointer to an integer. So this is the integer part, and putting the asterisk says I want to pointer to an integer. And then, I have a variable that I've called pscore. You do not have to start pointer variable names with a lowercase p. I'm just doing that here to differentiate between the pointer to the score and the score variable that I have up here. I will also point out that this asterisk doesn't have to be here. It can be here instead. That's personal preference. I prefer to have it after the data type that it's pointing to. But either way works. And in fact, over the course of this course, you've seen me put that in two different places. Now that I have a variable declared to be a pointer to an int, I want to give it a memory address. Because pointers are just memory addresses, and I want to give it the address of the score variable. So after I’ve executed this line of code, pScore holds the memory address of the score variable. When I print things out, I'll print two things. First, I'll print out the value of pScore. And this is the memory address, and it will be the same address as we see up here, the address of score. The other thing I'll do is print out what is at the pScore memory address. This is called de-referencing a pointer. And when we use the asterisk in this location, it's called the indirection operator. But you'll also hear people call it the de-referencing operator. And most of the time, people don't even talk about just the asterisk. They just talk about this as de-referencing the pointer. Because to be honest, just holding memory addresses isn't useful for most of the programs that we write. We often care about what's actually stored in those memory addresses. De-referencing a pointer says take this memory address and go tell me what's in that memory address. Because we know pScore points to an integer, we know that we should format the contents of that memory address as an integer, because it points to an integer. So when we find out what it points to, we should print it out as an integer. When we run the code We see that the value of the score variable is 500, and that's good because that's what we initialized it to. We see that the memory location that was allocated for the score variable is right here. And we see that the size of score is 4. So on this computer, with this compiler, I get four bytes for my int. Our score pointer has a value of this, which is exactly the address of score. Because pointers store memory addresses, we've output this as a memory address. And then, when we de-reference the pointer, when we say what's in this memory address, we find that 500 is what's in that memory address. And that's exactly what we expected because this is the address of the score variable, and we know the score variable has a value of 500. To recap, in this lecture we learned that pointers are variables that hold memory addresses. We saw how to assign a memory address to a pointer. And we saw how to de-reference the pointer. And we also learned early in the lecture that a pointer that has a value of null is not actually pointing to a valid memory location.