1
00:00:00,630 --> 00:00:09,200
OK, so let's go ahead and pick up where we left off last time, we were kind of finishing up with us,

2
00:00:09,210 --> 00:00:13,920
changing the values that were stored at the memory addresses.

3
00:00:15,520 --> 00:00:20,470
Of these pointers, right, so this point is stored in memory address, they are pointing to a specific

4
00:00:20,470 --> 00:00:22,020
piece of memory and we changed.

5
00:00:22,030 --> 00:00:27,280
We kind of like print out the garbage values, we print out the addresses and then we change the values

6
00:00:27,280 --> 00:00:28,510
and print out these new values.

7
00:00:29,630 --> 00:00:37,880
We also d allocated the memory that was reserved for our dynamically allocated mind dynamic pointer

8
00:00:39,770 --> 00:00:46,490
kind of the address that my dynamic pointer store was dynamically allocated memory on the heap, right?

9
00:00:47,510 --> 00:00:50,060
And we got rid of that here by allocating it.

10
00:00:50,840 --> 00:00:52,730
So we're going to continue on with this stuff.

11
00:00:52,800 --> 00:00:56,270
We're actually going to introduce a function.

12
00:00:58,070 --> 00:01:05,300
So what I'm going to do is just make some void function and I will just I'm just going to publish my

13
00:01:05,300 --> 00:01:15,440
function and I'm going to just going to say that it takes a pointer, an integer pointer as a variable.

14
00:01:15,440 --> 00:01:19,850
So I'm just going to say like a pointer bar.

15
00:01:21,020 --> 00:01:26,090
And then what it does is it just says, let's see.

16
00:01:28,670 --> 00:01:29,320
Let's see.

17
00:01:29,330 --> 00:01:31,090
We'll say pointer bar.

18
00:01:31,100 --> 00:01:32,510
So we'll reference that

19
00:01:35,030 --> 00:01:38,600
equals, you know, 20 or something like that we like.

20
00:01:38,600 --> 00:01:42,590
We just literally change it to a new a new number or something like that.

21
00:01:45,030 --> 00:01:50,880
So, yeah, a pretty basic function there, but so in this in this case, we're going to be passing

22
00:01:50,880 --> 00:02:02,130
a pointer to the function and in the parameters here you notice I say end star pointer bar, so we're

23
00:02:02,130 --> 00:02:03,780
not d referencing it.

24
00:02:05,560 --> 00:02:12,820
We're actually just saying that the data type is going to be pointed, that points to an integer here,

25
00:02:12,820 --> 00:02:14,110
so that's what all this means.

26
00:02:14,950 --> 00:02:16,500
So it's kind of similar to this, right?

27
00:02:16,510 --> 00:02:17,830
This is when we declared.

28
00:02:19,340 --> 00:02:19,910
A pointer.

29
00:02:20,910 --> 00:02:25,710
This is also just saying like this is the type of thing that's getting passed here, it's a variable

30
00:02:26,130 --> 00:02:33,750
that is of type pointer, it's a pointer and it points to an integer type, although that's remember

31
00:02:33,750 --> 00:02:34,620
that's kind of like.

32
00:02:35,780 --> 00:02:38,840
Not that important, right, because the point is just an address.

33
00:02:39,680 --> 00:02:47,060
But it's just kind of defines what we're passing here, and of course, we can use this variable right

34
00:02:47,060 --> 00:02:48,500
here to now.

35
00:02:48,500 --> 00:02:53,090
Do you reference it and store 20 at that address instead?

36
00:02:55,220 --> 00:02:55,610
Cool.

37
00:02:55,790 --> 00:03:00,350
So what I'm going to do is, of course, call this function down here, but I want to kind of point

38
00:03:00,350 --> 00:03:02,750
out a a few things.

39
00:03:05,160 --> 00:03:10,680
And I'm actually going to I'm going to get rid of some of this stuff right here, just so we can.

40
00:03:12,000 --> 00:03:13,140
Have some more space.

41
00:03:15,330 --> 00:03:19,830
So what I'm going to do here is actually.

42
00:03:23,870 --> 00:03:30,310
Kind of show you what it would be like passing things to this if we didn't necessarily have a pointer

43
00:03:30,320 --> 00:03:30,800
already.

44
00:03:30,800 --> 00:03:35,210
So I have this thing called my pointer, but what if I just have something this isn't?

45
00:03:36,170 --> 00:03:41,480
My aunt equals eight or something like that, right?

46
00:03:42,260 --> 00:03:46,520
So I can go here and I will see out my aunts.

47
00:03:48,470 --> 00:03:52,250
And you know, it's just going to be eight at this point.

48
00:03:52,250 --> 00:03:59,900
But then what I want to do is, I mean, we want to call this void function, my function, and I say,

49
00:03:59,900 --> 00:04:07,490
Hey, I want to pass my integer to this function and I'm hoping to change the value of my integer.

50
00:04:07,490 --> 00:04:14,780
So I go ahead and I say, you know, I passed my into the function and then I pointed out and I say,

51
00:04:15,980 --> 00:04:18,320
you know, print out my int.

52
00:04:19,810 --> 00:04:20,140
Right.

53
00:04:21,160 --> 00:04:25,390
So I'm going to go ahead and do that and say a line, and then I save it.

54
00:04:28,140 --> 00:04:29,860
I'm going to go ahead and compile it.

55
00:04:30,320 --> 00:04:32,110
Oh, look, there's an air.

56
00:04:33,710 --> 00:04:34,460
Interesting.

57
00:04:35,600 --> 00:04:40,610
Note initializing argument, one of my function void.

58
00:04:43,060 --> 00:04:48,000
So says air invalid conversion from end to end pointer.

59
00:04:49,150 --> 00:04:54,760
So you notice that the thing is is that we specified that a pointer was going to come here.

60
00:04:54,760 --> 00:04:56,890
We wanted to pass a pointer to this function.

61
00:04:58,240 --> 00:05:00,250
But here we just passed an integer.

62
00:05:00,250 --> 00:05:09,100
So of course, if I was to pass, you know, my pointer here was, yeah, it's just my pointer.

63
00:05:09,400 --> 00:05:15,100
If I was to pass my pointer here, which is this one right here and it's an actual pointer.

64
00:05:17,500 --> 00:05:20,650
If I compile this, you know, so we don't get a problem, it compiles fine.

65
00:05:21,890 --> 00:05:23,390
That's because it's already a pointer.

66
00:05:23,810 --> 00:05:29,690
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

67
00:05:29,690 --> 00:05:31,070
still do this?

68
00:05:31,070 --> 00:05:37,790
But what we have to do is something that I kind of showed briefly in the first kind of virtual memory,

69
00:05:37,880 --> 00:05:41,990
not the first virtual memory lecture, but the one that was like, I think, two lectures ago where

70
00:05:41,990 --> 00:05:45,080
we talked about the heap and stuff like that.

71
00:05:45,080 --> 00:05:49,400
I showed this single ampersand to get the address of something, right?

72
00:05:49,820 --> 00:05:54,620
So what we could actually do is say, Ampersand my end.

73
00:05:56,300 --> 00:06:03,110
And to kind of make sure you're not too confused on this, I know that you've seen the ampersand for

74
00:06:03,110 --> 00:06:08,750
past by reference, but that was when it was up in the parameters here.

75
00:06:08,990 --> 00:06:10,830
These are the arguments, right?

76
00:06:10,850 --> 00:06:13,340
This is the function arguments here that I've highlighted.

77
00:06:13,550 --> 00:06:19,010
This is the parameters of the function definition when we have if we were to have an integer here,

78
00:06:19,010 --> 00:06:25,430
this is considered past by reference do not totally disregard was highlighted right here for the time

79
00:06:25,430 --> 00:06:25,880
being.

80
00:06:25,880 --> 00:06:32,000
Just while I talk about the parameters, if this if you see this right here, this is passed by reference.

81
00:06:32,000 --> 00:06:33,920
So that's something we've already talked about, right?

82
00:06:34,970 --> 00:06:41,240
But if you have this, so let's focus on this down here with this ampersand in the function arguments,

83
00:06:41,660 --> 00:06:46,760
this is saying get the address of my integer.

84
00:06:47,640 --> 00:06:49,890
Wherever my integer is stored.

85
00:06:50,070 --> 00:06:55,440
Get that address, location and pass that as an argument up here.

86
00:06:55,440 --> 00:07:00,660
So the parameter is now going to be an address that is going to work out fine.

87
00:07:00,660 --> 00:07:01,350
And why?

88
00:07:01,360 --> 00:07:07,290
Because we said that we we're going to have a pointer here and remember back, what is a pointer?

89
00:07:07,320 --> 00:07:09,300
A pointer is just an address, right?

90
00:07:09,310 --> 00:07:16,560
So if we send the function and address and we kind of catch that data here in the parameters and we

91
00:07:16,560 --> 00:07:22,650
say, Hey, we're expecting a pointer C++, it's like, OK, well, a pointer is an address and you

92
00:07:22,650 --> 00:07:23,580
sent me an address.

93
00:07:23,580 --> 00:07:24,330
So that's fine.

94
00:07:25,410 --> 00:07:28,380
So let's go ahead and save this and try to compile this.

95
00:07:29,640 --> 00:07:32,040
And you notice that this compiles fine, too.

96
00:07:32,700 --> 00:07:37,970
That is because, like I just said, we are now passing the address of my integer.

97
00:07:39,130 --> 00:07:42,800
And so let's go ahead and run this to see what happens.

98
00:07:42,880 --> 00:07:46,500
We said it was eight, then we print that out, right?

99
00:07:46,510 --> 00:07:49,030
So it'll say eight, we send it to my function.

100
00:07:49,540 --> 00:07:53,230
So we send the address up here, right?

101
00:07:54,190 --> 00:07:58,150
Then what happens is we d reference that address.

102
00:07:59,110 --> 00:08:01,540
Because it's now seen as a pointer.

103
00:08:02,230 --> 00:08:06,610
We've sent the address up here and it caught it and it says, OK, a pointer is an address.

104
00:08:06,610 --> 00:08:07,660
You sent me an address.

105
00:08:07,660 --> 00:08:09,130
I'm going to use this.

106
00:08:10,330 --> 00:08:15,520
Pointer Bar that's mentioned here with the address you sent me, and I'm going to consider it a pointer

107
00:08:15,520 --> 00:08:19,150
that holds that address and this line, I'm going to reference it.

108
00:08:19,360 --> 00:08:24,820
So I have that memory location area and I go ahead and take the 20 and I saw the toe in there.

109
00:08:25,630 --> 00:08:31,730
Now, when we print out my end, my end should not contain it anymore.

110
00:08:31,780 --> 00:08:35,140
She can contain 20, so let's go ahead and verify that.

111
00:08:38,190 --> 00:08:41,270
And we see that it in fact changed straight.

112
00:08:41,670 --> 00:08:43,410
It was eight right here.

113
00:08:45,720 --> 00:08:50,900
And then what we did is we printed that out, we went ahead and called my function.

114
00:08:50,940 --> 00:08:53,630
We passed the address of this integer.

115
00:08:53,640 --> 00:08:54,510
It went up here.

116
00:08:54,510 --> 00:08:57,690
It said, OK, I'm going to use this address as a pointer.

117
00:08:57,690 --> 00:08:59,220
I'm going to d reference that pointer.

118
00:08:59,220 --> 00:09:00,750
So I'm d referencing the address.

119
00:09:01,080 --> 00:09:03,420
Take 20, overwrite the value eight.

120
00:09:04,020 --> 00:09:07,170
Now when we go back down here and we print out my intent, it's not.

121
00:09:07,200 --> 00:09:09,570
It's not eight anymore and stores the value 20, right?

122
00:09:10,200 --> 00:09:11,340
That's what that integers.

123
00:09:13,200 --> 00:09:13,950
All right, cool.

124
00:09:13,980 --> 00:09:19,350
So this was kind of a really short lesson, we're going to continue on, I'm trying to break them up

125
00:09:19,350 --> 00:09:21,360
into pieces, so it's not too overwhelming.

126
00:09:21,360 --> 00:09:23,120
But with that, I will see you in the next lecture.

127
00:09:23,130 --> 00:09:27,150
We will continue on and we'll keep looking at a few more things with pointers.
