MyProgrammingLab 9-2 Part 1 Transcript

Print

All right. Let's do the 9.2.

So first thing is login to My Programming Lab. And then click on your Week 1. And then we're going to go over to 9.2.

If you hit the Arrow, it brings everything down. You see that? You don't want to do that. Instead hit the Plus sign. So if you hit the Plus sign, it'll just open it up like this. And then that way we can open up just 9.2.

So go ahead and click on 9.2 and that opens it up. And then we're going to click on the first item to do, which is this one here. And it says declare a variable-- and call it IP-- that can be assigned the address of an int variable. In other words, declare IP to be a type pointer to an int.

So to create a pointer to an integer, you type int pointer, and then whatever you want to call it. So we call it IP. And then a semicolon at the end of the line to end the code. Right? So, this is how you create an int pointer.

Now at the same time, you can put a space bar like this. So some people like to put a space bar before and after the pointer so you really notice that it is a pointer. OK? And then, also, sometimes you will see the asterisk over here next to the variable. So this is an int that is a pointer to IP. And that's the logic behind it.

My personal preference is I like the pointer to be right next to it like that so the data type is int pointer. So this is an int pointer variable called IP. And that means that it is a memory location. It can hold a memory address to an integer. All right?

So now if I hit Submit, then we've got it correct. So now I'm going to click on the next one. The next one says, declare a variable CP-- so call it CP-- that can be assigned the address of-- so it needs to be able to hold a memory address-- of a character variable. In other words, declare CP to be of the type pointer to character. So this is a pointer to a character. That's the data type.

Then they want you to call it CP. So we go CP semicolon. So this is a pointer to a character and we're calling it CP. All right.

So now they want us to create a variable called DP that can be assigned the memory address of a double. So in other words, declare DP to be of the type pointer to a double. So a double pointer-- right-- a pointer to a double. And then call it DP. And then semicolon to end the line of code. So we hit Submit and there it is.

So it'll grab the next one. Assume that int variable 'counter' has already been declared. So counter is already in memory. So that means that-- and it's an int variable. So they said int counter-- and probably initialized it to some value like that. So this right here, this line, has already been created. We don't have to create it. OK?

And then it says, assume further that a variable counter pointer of type pointer to an int has already been declared. So it's a pointer to an int. And it's called counter pointer. And that's already been declared. OK? So this is already in memory. All right? So it has already been declared.

Now it says, write a statement that makes counter pointer point to counter. So they want this pointer to point to this memory location. So the way you do it, is you simply say, counter pointer is assigned the address of counter.

Because, remember, address of that ampersand means address of. OK? So the ampersand means address of. So that's going to get the memory address of counter.

And then we're going to put the memory-- we're going to assign the memory address. So this right here means 'is assigned'. So counter pointer is assigned the address of the counter variable. So this can have that memory address. All right?

So these two things were already done. So I can-- I'm going to go ahead and delete those out. And this right here is just a comment, so it should not hurt anything.

You know I'm going to leave it and I'm going to see if My Programming Lab is smart enough to ignore this, because it is a comment. All right. So I'm going to hit Submit. And you can see you are correct. All right? So there's the answer. Counter pointer, the pointer, is assigned the memory address-- the address of the counter variable.

Let's go to the next one. Assume that strike counter has already been declared to be a pointer to an int. All right?

So that means it's a pointer to an int. It's an int pointer. And it's called strike counter. So this right here has already been written. All right? And this is the assumption. Assume that this has already been written. All right?

Assume further that strike counter has been initialized. Its value is the address of some int variable. So, they're saying that strike counter has been initialized to the value of some variable. So let's go ahead and just call it, maybe, strikes or something like that. All right?

So the address of some variable is assigned to strike counter. All right? And initialize-- the word initialize means, has been given an initial value. All right?

Now write an expression whose value is four times the value of the variable that strike counter is pointing to. All right? So they want the value to be four times whatever strike counter's pointing to.

So how do we come up with that? Well, the first thing you have to do is you have to de-reference the pointer. You have to get the content of strike counter. All right?

So to get the content at the pointer, you have to de-reference a pointer. That right there means de-reference the pointer. Another way to think of it is this right here means get the content at that pointer. All right?

So let's put a little note over here. The asterisk means-- and I should say when the pointer-- if the asterisk is here, that means create a pointer. And that's when you're creating the variable. So the asterisk up here means create a pointer. So asterisk up here means create a pointer in this context.

And what I mean by that is, when you are creating a variable. So if you are creating a variable, the asterisk means create a pointer of that type. So it's an int type. So this can point to an integer variable.

The asterisk down here-- the content has already been created-- the variable has already been created. So if I say, cout like this and then I say endl, like that, this will display the content at that pointer address. So the asterisk here means-- the asterisk in this context means get content at pointer address or de-reference the pointer.

And you're going to hear people say de-reference the pointer a lot, so you want to get comfortable with that. Because that's our lingo. That's our software development lingo. Right?

But another way to think about it is it means get the content at that pointer address. All right? So if the variable has already been created, when you put an asterisk it means get the content at. If the variable is being created, it means create a pointer of that data type. OK?

It's the same asterisk, but it's kind of like the word 'there'. If I say, 'go over there', that means one thing. And if I say, 'their car', that means a different thing. It's the context. All right? So both of those two words are 'there' but based on the context, the spelling is going to be different, and the meaning is different.

So with this right here, it's the exact same scenario. If it's creating a variable, that means create a pointer of that data type. If it's already been created, that means get the content at that pointer. All right?

So now what they're asking is for us to just get the value that's four times the value of the variable. So this right here gets the value at that memory address, but they want four times that. So I'm going to say times 4. All right?

And they don't want the cout part. I was just talking. I put the cout just to talk you all through it. All right? So I'm going to go ahead and get rid of that.

So all they're after is this line right here. All right? Because they're saying that this has already been done. And they're saying that this has already been done. So I'm going to go ahead and get rid of those two items, because it says, assume that those things have already been done. All right. So assume that it has already been declared, and it's already been initialized.

So now all they want is an expression whose value is four times the value of the variable that strike counter is pointing to. So this is the value of the variable that strike counter's pointed to, because we got the content at that memory address. And then they want four times that, so we're going to say times 4. All right?

And we don't need a semicolon or anything in this, because they just want the expression and that's all. So I'm going to hit Submit. And we've got it.

[End of Audio]