MyProgrammingLab 9-3 Transcript

Print

All right, let's do 9.3. Let's open this up. And if you hit this arrow, it will actually pop everything open, which is not what we're after. See that? So instead of hitting the arrow, hit the plus sign. And I hit the arrow another time, for it to collapse. So instead of hitting that arrow, instead hit the plus sign.

Then go to your Week 1, and hit the plus sign on that. And then we're going to open up Chapter 9. And then we're going to go to 9.3, where we hit the plus sign. And there are three items to do. So we'll click on the first one.

And it says, int has been declared. And it's a pointer to an integer. And enrollment has been declared, and it is an array of 20 elements. Write a statement that makes int point to the first element of the array.

All right, so how do we get the first element of the array? The array is called Enrollment. The first element is 0, because remember, were 0-based. So that is the first element of the array.

Now, we want int to point to the first element of the array. So we need to get the address of that first element. So that's the answer, right there. So our pointer is going to be assigned the address of the first element of the array.

All right, let's look at the next one. The next one says, the pointer has already been created and the Enrollment array has been created with 20 elements. It says, write a statement that makes IP point to the last element of the array. So how do we get the last element of the array?

The array is called Enrollment. And there's 20 elements. We are 0-based, so that means that the first element is at position 0. Well then, that means that the last element is at position 19, because 0 all the way up to 19 is a total of 20 elements. So that's the position of the last element of the Enrollments array.

So we want IP to point to this last one. And so I need to get the address of that last item in the array, and then point IP to it. So the address of the last item is assigned into the variable IP. Or the way to actually read it is, IP, the pointer IP, is assigned the address of the last element of the array.

IP has been created. And Enrollment has been created with 20 elements. And then also, a variable called Section has been declared. And Section is an integer, and it has some value between 5 and 10 inside of it. So write a statement that makes IP point to the element in the array indexed by section.

So the array is called Enrollment. And then how do we get the element that is indexed by section? Well, if I put a 0 right there, that's going to give me the first one. If I put a 1, it's going to give me the second one. 2 is going to give me the third one. 3, like that all the way up to 19, right? So that's how I get all the items out of the array.

But we have a variable called Section. So instead of putting in a hard number like that, we're just going to type in the variable Section. So that's going to give me the item out of the array that is indexed by section. Section is a number between 5 and 10, all right?

Now, we want IP to point to that item in memory. So IP is going to point to the address, is going to be assigned the address of that item. And then I need to put a semicolon at the end. And that's how we do it. So IP is assigned the address of-- because remember, pointers can only hold memory addresses. That's all they can hold, hexadecimal memory addresses. So I need to get the memory address of the item being indexed by section.

And that is 9.3.

[End of Audio]