OK, so let's go ahead and pick up where we left off last time, we were kind of finishing up with us,
changing the values that were stored at the memory addresses.
Of these pointers, right, so this point is stored in memory address, they are pointing to a specific
piece of memory and we changed.
We kind of like print out the garbage values, we print out the addresses and then we change the values
and print out these new values.
We also d allocated the memory that was reserved for our dynamically allocated mind dynamic pointer
kind of the address that my dynamic pointer store was dynamically allocated memory on the heap, right?
And we got rid of that here by allocating it.
So we're going to continue on with this stuff.
We're actually going to introduce a function.
So what I'm going to do is just make some void function and I will just I'm just going to publish my
function and I'm going to just going to say that it takes a pointer, an integer pointer as a variable.
So I'm just going to say like a pointer bar.
And then what it does is it just says, let's see.
Let's see.
We'll say pointer bar.
So we'll reference that
equals, you know, 20 or something like that we like.
We just literally change it to a new a new number or something like that.
So, yeah, a pretty basic function there, but so in this in this case, we're going to be passing
a pointer to the function and in the parameters here you notice I say end star pointer bar, so we're
not d referencing it.
We're actually just saying that the data type is going to be pointed, that points to an integer here,
so that's what all this means.
So it's kind of similar to this, right?
This is when we declared.
A pointer.
This is also just saying like this is the type of thing that's getting passed here, it's a variable
that is of type pointer, it's a pointer and it points to an integer type, although that's remember
that's kind of like.
Not that important, right, because the point is just an address.
But it's just kind of defines what we're passing here, and of course, we can use this variable right
here to now.
Do you reference it and store 20 at that address instead?
Cool.
So what I'm going to do is, of course, call this function down here, but I want to kind of point
out a a few things.
And I'm actually going to I'm going to get rid of some of this stuff right here, just so we can.
Have some more space.
So what I'm going to do here is actually.
Kind of show you what it would be like passing things to this if we didn't necessarily have a pointer
already.
So I have this thing called my pointer, but what if I just have something this isn't?
My aunt equals eight or something like that, right?
So I can go here and I will see out my aunts.
And you know, it's just going to be eight at this point.
But then what I want to do is, I mean, we want to call this void function, my function, and I say,
Hey, I want to pass my integer to this function and I'm hoping to change the value of my integer.
So I go ahead and I say, you know, I passed my into the function and then I pointed out and I say,
you know, print out my int.
Right.
So I'm going to go ahead and do that and say a line, and then I save it.
I'm going to go ahead and compile it.
Oh, look, there's an air.
Interesting.
Note initializing argument, one of my function void.
So says air invalid conversion from end to end pointer.
So you notice that the thing is is that we specified that a pointer was going to come here.
We wanted to pass a pointer to this function.
But here we just passed an integer.
So of course, if I was to pass, you know, my pointer here was, yeah, it's just my pointer.
If I was to pass my pointer here, which is this one right here and it's an actual pointer.
If I compile this, you know, so we don't get a problem, it compiles fine.
That's because it's already a pointer.
But what if we do want to, like change my integer and I don't want to make it a point to where we can
still do this?
But what we have to do is something that I kind of showed briefly in the first kind of virtual memory,
not the first virtual memory lecture, but the one that was like, I think, two lectures ago where
we talked about the heap and stuff like that.
I showed this single ampersand to get the address of something, right?
So what we could actually do is say, Ampersand my end.
And to kind of make sure you're not too confused on this, I know that you've seen the ampersand for
past by reference, but that was when it was up in the parameters here.
These are the arguments, right?
This is the function arguments here that I've highlighted.
This is the parameters of the function definition when we have if we were to have an integer here,
this is considered past by reference do not totally disregard was highlighted right here for the time
being.
Just while I talk about the parameters, if this if you see this right here, this is passed by reference.
So that's something we've already talked about, right?
But if you have this, so let's focus on this down here with this ampersand in the function arguments,
this is saying get the address of my integer.
Wherever my integer is stored.
Get that address, location and pass that as an argument up here.
So the parameter is now going to be an address that is going to work out fine.
And why?
Because we said that we we're going to have a pointer here and remember back, what is a pointer?
A pointer is just an address, right?
So if we send the function and address and we kind of catch that data here in the parameters and we
say, Hey, we're expecting a pointer C++, it's like, OK, well, a pointer is an address and you
sent me an address.
So that's fine.
So let's go ahead and save this and try to compile this.
And you notice that this compiles fine, too.
That is because, like I just said, we are now passing the address of my integer.
And so let's go ahead and run this to see what happens.
We said it was eight, then we print that out, right?
So it'll say eight, we send it to my function.
So we send the address up here, right?
Then what happens is we d reference that address.
Because it's now seen as a pointer.
We've sent the address up here and it caught it and it says, OK, a pointer is an address.
You sent me an address.
I'm going to use this.
Pointer Bar that's mentioned here with the address you sent me, and I'm going to consider it a pointer
that holds that address and this line, I'm going to reference it.
So I have that memory location area and I go ahead and take the 20 and I saw the toe in there.
Now, when we print out my end, my end should not contain it anymore.
She can contain 20, so let's go ahead and verify that.
And we see that it in fact changed straight.
It was eight right here.
And then what we did is we printed that out, we went ahead and called my function.
We passed the address of this integer.
It went up here.
It said, OK, I'm going to use this address as a pointer.
I'm going to d reference that pointer.
So I'm d referencing the address.
Take 20, overwrite the value eight.
Now when we go back down here and we print out my intent, it's not.
It's not eight anymore and stores the value 20, right?
That's what that integers.
All right, cool.
So this was kind of a really short lesson, we're going to continue on, I'm trying to break them up
into pieces, so it's not too overwhelming.
But with that, I will see you in the next lecture.
We will continue on and we'll keep looking at a few more things with pointers.