All right. So let's go to the next one. This one says assume the strike counter has already been declared, again, right? And then it says assume that strike counter has been initialized as value points to some variable. Now it says write a statement that assigns the value 27 to that variable. That is the strike counters 22. So the first thing, is you have to get the content at that variable. So we have to get the content at straight counter variable.
So this right here says dereference the pointer, so get the content at that memory address. And now, instead of looking at that content we're going to overwrite it with the number 27. So we're going to assign the value 27, we're going to assign 27 to the variable that strike counter's pointing to. So we're going to dereference the pointer and we're going to get the content at that pointer and we're going to overwrite it. We're going to assign the value 27 to it. See how that works?
All right, so let me hit submit, and that's how it works there. OK, so just remember, dereference the pointer and that gets the variable at that pointer. And then, now that we have the variable at that pointer, we're going to assign 27 to it. Let's go to the next one.
Assume the strike counter has already been declared to be a pointer to an int, and also assume that it's pointing to some integer. It's an integer variable. Write a statement that adds 22 to the value of the variable. So we're going to it dereference the pointer. So that gets the variable that the pointer is pointing to. And then we're going to add 22 to that value. That's all they're asking for.
I made a mistake. OK, got it, missing a semicolon. So they want the full statement. Now if you get an error, just kind of read it, and they give you hints. For example, here they want a semicolon and they're also expecting the assignment operator, and that means an equal sign, right? Because the equal sign no longer means equal to software developers like us, now it means is assigned. All right?
So let's go back over to the work area. So this is where it took us, you want to click on work area, right there. And then we're going to do this again. So they want to add 22 to the value. So we're going to go ahead and say, get the variable that the pointer is pointing to, and then we're going to add 22 to it, and then once we add 22 to it, we're going to assign the value to the variable that the pointer is pointing to. So we're going to dereference the pointer. We're going to sign to it the value that it currently has, plus 22. All right? So remember, the right side is done first, and then once it's fully completed, it's going to be assigned to the left side. So we get the value of the variable that pointer is pointing to. So we're going to dereference the pointer. We're going to add 22, to it and then we're going to assign it to the variable that the pointer is pointing to. That's how it should look. All right, let's hit submit, and now we're good. So just remember, the right side is done first, and then it's assigned into the left side.
Assume that IP 1, 2, and 3 have already been declared as pointers. Assume that they also have been initialized so that they point at variables. And then write a statement that computes the sum of the variables that IP and IP2 point to, and then assigns that value to the variable that IP 3 points to. So the first thing we need to do, is we need to dereference IP3. All right? So we've got the variable that the pointer is pointing to. Now that we have the variable that the pointer is pointing to, we're going to assign to that the sum of IP2 and IP3, whatever they're pointing to.
Now we have to dereference IP1, and then add to it, and then we're going to dereference IP2, semicolon. So I'm going to get the content at IP1. I'm going to add that to the content of IP2. That's going to add the two contents together, and once they add it together, we're going to assign that to the content of IP3. So the variable that IP3 is pointing to.
All right, the variables XP and YP have both been declared, and they're pointing to integers. And they have been assigned values. Write the code to exchange the values of these two variables. So, whatever value XP is pointing to should become the value that YP is pointing to. So we're going to come over here, and then here's a typical error. Sometimes a person will say XP is assigned the value of YP, and then YP is assigned the value of XP. However, if you do that, this line right here wipes out XP, and puts into its place the value of YP. So that's a problem, because now XP and YP are identically the same. So this right here has no effect at all, because they're both identically the same.
So let's take just a quick look at what I'm talking about here. I just created a little small project to do some testing.
And if I come over here and I say int pointer XP, and that's going to be assigned the address of first. Let's go ahead and make first a variable. So we're going to say int first, and we're going to make that 27. And then we're going to say int second, and make that 13. And then XP, we're going to give it the address of first. And then we're going to do YP, and YP is going to be the address of second, like that.
OK, now it's saying flip the content. So XP is pointing to first, YP is pointing to second. They're saying they want to flip the content. So they want first to have 13 in it, and they want second to have 27 in it. So if you say the dereference XP and then assigned to it dereference YP like that. And then you say dereference YP and assign dereference XP, like that, this line right here is going to wipe out XP. So now they're going to be both identically the same. So right now if I say c out, and I say first, and then I show you what is in first, and then I say c out, and then I say second, and then I show you what it is and second, like that, watch what happens.
Look at that, they're both 13. Now why is that true? Because XP right here, XP is getting the content, is being assigned the content, of what YP is pointing to, which is 13. So at this point, XP and YP are both pointing out variables that have 13 in them. OK? Now just as a reminder for debugging, I can actually put a break point right here to show you what's happening, and then I can run it, and it's going to run until it hits that break point.
And then here, you can see XP is a memory address. You see that? It's pointing to the memory space and the memory address of first. Now, first has a value of 27 in it. You can see the 27 right there. OK? Now I'm going to go ahead and step over. Remember the step over? So I'm going to hit step over right there. And then I'm going to hover my mouse over second. And you can see YP has a memory address that's pointing to second. So the content at that memory address is 13. And you can see it's pointing to that right there.
Now on this line right here, again, if I hover my mouse over YP, you can see it's 13. XP at this moment in time is 27, but the content that XP is pointing to, is going to be overwritten by, it's going to be assigned, the content that YP is pointing to it, which is 13. So watch this. When I hit step over, there is my step over for debugging. Now, if I hover my mouse over XP it's now 13. But YP is also 13. So now if I say YP is assigned, the content is assigned, whatever the content is of XP, XP is now 13. So they're both 13. So my 27 has been wiped out.
All right, so I'm going to hit stop, and then the question is, how do I get around that? And what you have to do is, you have to create a third variable. So I'm going to say int temp. And then I'm going to assign to it the content. Whoops, I don't want to do it right there because the variables have not been created at that point. I want to do it right here. So I'm going to create a variable called temp, and I'm going to assign to it the content of XP. So the temp variable can hold an int, and then the content of XP, I'm going to assign it into temp.
So now temp has 27 in it. Then the content of x.p. Is going to get wiped out, and is going to be assigned the content of YP, which is 13. And then I'm going to assign the content of YP temp.
So this is kind of like a shell game. I create a variable, and then I assign the first one into that variable. Then I assign the second one into the first one. And then I assign the temporary variable into the second one. And that's how we flip, that's how we flip values. All right? So you have to remember to create a temporary variable so that you don't wipe out your data. And that's a very, very common error. That's why you want to be careful with this. All right? So let's go back over to my programming lab.
[End of Audio]