MyProgrammingLab 9-4 Transcript

Print

All right. Let's do 9.4. So if I hit the plus sign next to 9.4, and I go to the first one. And then here assume that IP has been declared as a pointer to an int. And the result has been declared as an array of 100 elements So that means we have int pointer IP. And then we have int results with 100 elements as an array. And so let's say we initialize it to 1731 42 so forth for 100 position. So we have these two items going on.

Further, IP has been initialized to point at an element in the first half of the array. So that means that IP is pointing to a specific element and result in the first half of the array. So let's say at position 25. It could be anywhere in the first half of the array. Now this right here would crash because result 25 is an actual number. It's not a memory address. So to point at this item, we have to an address of in front of it like that. All right? So IP is assigned the address of an item in the array somewhere in the first half.

Now write an expression whose value is the element in the array after the element the IP points to. And here is what we're talking about. If you want to get the next item, you say IP plus one. And that takes you to the next item. And this is pointer arithmetic. And this is what they want you to learn about. And that is that if you add one to the memory address to the pointer, it does not add one byte. Instead it adds one unit. Now what unit? Whatever it's pointing to. So if the data type is an int. That means it's going to add four bytes of memory. So the memory address is going to go up by four bytes because it's an int.

If it's a double, then adding one like this will add eight bytes, because a double is eight bytes wide. So if I say IP plus one, that's going to add four bytes. That's going to add one unit, one int. And that means it's going to point to the next item in the array. Because when you create an array, all the items, all 100 items, are all put contiguously. They're next to each other. Each item is right next to each other in the array.

So when you add one, it's going to point to the next item of the array. Now they want you to pull the value. So that means that we have to add one first. So I'm going to put it in parentheses. And then we have a pull of value. So I'm going to say, get the content at that item. So I'm going to de-reference the pointer after I add one to the pointer. And that right there is going to give us the value.

So if I wanted to display that value, I could say c-out, arrow, arrow. And then arrow, arrow, [? endal. ?] And that would actually display that value. But they don't want that value-- they don't want it displayed like this. Instead, they just want the expression that gets it. And that's it. So all they want is the expression that pulls of value, and that's it.

All right. So this part right here I was just explaining what's going on behind the scenes. And what they want is that right there. So I'm going to hit submit. Correct. All right, let's click on the next one. Assume the IP has been declared, and it is a pointer. So, again, we have a pointer, a int pointer, called IP. Now that result has been declared, and it is an array of 100 elements. So it's an int array called result with 100 elements.

And, for example, I could load the elements with a four loop. I could load the elements using a read from a file. I could also load the elements by using curly braces. So if you remember from your last class, you can load the array-- you can create the array and load it in one single line by putting the values that you wanted to load into in curly braces. So open curly brace, close curly brace, semicolon.

And then inside of here, I would then need to put 100 items-- 100 integers in this case because an array is an integer. So 37, 42. And obviously I'm not going to type 100 values. But if I were going to create it, and I wanted to load it one single line, I would need to type 100 values. Or I could read the values from a file. I could use a randomizer to create the values. There's a lot of different ways to do it. But the important part is that we have array that's 100 in size that has been loaded.

And IP has been initialized to point at an element in the first half of the array. So IP is pointing to the address of the element in the first half. So it could be anywhere, right? So I'm just going to use 30, just to have a number. And I'm pulling the address of that element so that my pointer is pointing to that element. All right. Now it says, write a statement that makes IP point to the next element of the array.

So instead of IP pointing to the 30th element, they want IP to point to the 31st element. But they want you to do it using pointer arithmetic. And here's how you do it. You simply say IP is assigned what IP used to be plus 1. And I'm done. That's all they want.

So if I add one to IP, it's going to add one unit to IP. And in this case, the unit is an integer. So that means it's going to add one integer size to IP. So the memory address is going to increase by 4 bytes, because an integer is four bytes. All right? That's how pointer arithmetic works.

And, again, if I'm using a short, adding one to it would increase by two bytes. Because a short is only two bytes large. So depending upon the data type is going to be the number of bytes it increases when you do a plus one. All right? And that's pointer arithmetic. So what they're looking for is this line right here. So I'm going ahead and get rid of everything else.

So now assume that IP and JP have both been declared to be pointers to integers. And that result is an array of 100 items. Assume that IP has been initialized to point to an element in the first half of the array. Write a statement that makes JP-- JP is the other pointer-- point to the element of the array just after the one that IP points to.

So all I do is simply say, JP is assigned where IP points plus one unit. Just like that. So JP is now pointing to the next element. Because if I say the pointer plus one, that's going to add one unit. In this case we're talking about ints, so that's going to add four bytes. So that's going to point to the next item in the array. So JP is assigned where IP points plus one unit.

All right, assume that IP and JP have both been the declared to be pointers to ints. So int pointers. And the result is an array of 100 elements. Assume that IP has been initialized to point at an element in the first half of the array. Write a statement that makes JP point to the element in the array five positions after the one that IP points to.

So all I do is simply say IP plus five. So with pointer arithmetic, that means that it's going to move five units to the right. So it's going to point to the fifth unit after JP. So JP points to this one. Now we're adding five units to it. So it's going to move five items over to the right.

And that is 9.4.

[End of Audio]