1
00:00:00,960 --> 00:00:05,760
So this time around, I want to look at something you may never have heard of before, if you come from

2
00:00:05,760 --> 00:00:10,650
a language like and you've definitely never, never heard of, if you've never done any programming

3
00:00:10,650 --> 00:00:11,010
before.

4
00:00:11,580 --> 00:00:20,190
But it's the concept of pointers and pointers are incredibly useful and they are often a source of some

5
00:00:20,190 --> 00:00:21,810
confusion to people.

6
00:00:22,440 --> 00:00:26,370
But they're not that hurt, particularly Engo, which handles them really, really well.

7
00:00:26,820 --> 00:00:34,770
So we have seen variables already like ver my strength and that is a string and I'm going to store value

8
00:00:34,770 --> 00:00:35,490
in my string.

9
00:00:35,490 --> 00:00:36,870
I call my string.

10
00:00:36,990 --> 00:00:39,780
Has the value of green a color.

11
00:00:40,200 --> 00:00:42,030
OK, so I'm not using it yet.

12
00:00:42,030 --> 00:00:42,660
Still in error.

13
00:00:42,660 --> 00:00:45,780
So let's just print that out so we don't have an error log print line.

14
00:00:47,710 --> 00:00:54,670
My string is set to and then a comma and then the actual value, etc., which is just the variable itself,

15
00:00:55,000 --> 00:00:55,510
no problem.

16
00:00:55,690 --> 00:00:57,610
So if I run that, you know what's going to happen?

17
00:00:57,610 --> 00:00:58,330
It's going to print.

18
00:00:58,330 --> 00:01:02,770
My string is set to green with a capital G and it does.

19
00:01:02,810 --> 00:01:06,330
OK, no surprises so far and nothing about Pointer so far.

20
00:01:06,730 --> 00:01:15,220
Let's come down here and create a new function funk and I'm going to call it change using a pointer,

21
00:01:15,460 --> 00:01:18,120
which is not a great name, but it's exactly what I'm going to do.

22
00:01:18,520 --> 00:01:24,290
It will take one parameter which I'll call S, and it is a type string, but it's not an ordinary string.

23
00:01:24,310 --> 00:01:29,440
It has an asterisk in front of it and it is a type of pointer to a string.

24
00:01:30,230 --> 00:01:32,500
OK, so you can't see that because it's great out.

25
00:01:32,510 --> 00:01:34,330
So let's actually do something here.

26
00:01:35,080 --> 00:01:35,830
New value.

27
00:01:37,060 --> 00:01:41,380
I'm going to create a new variable called New Value and I'm going to call it store that the value of

28
00:01:41,380 --> 00:01:49,270
red with the capital R and I'm going to say my argument, which is s I'm going to say a star in front

29
00:01:49,270 --> 00:01:49,830
of that.

30
00:01:50,200 --> 00:01:53,530
So a pointer to S is given new value.

31
00:01:53,890 --> 00:01:55,030
I've just decided read to it.

32
00:01:55,030 --> 00:01:57,430
I could have just put red here, but I'm just making it light up.

33
00:01:57,880 --> 00:02:02,560
And then after I print that line up here, let me just try to.

34
00:02:03,880 --> 00:02:09,520
Put it like this, I'm going to call that function, I will call change using pointer and I will pass

35
00:02:09,520 --> 00:02:10,080
in.

36
00:02:10,390 --> 00:02:16,930
I can't pass in my mind variable called my string because that'll give me an error, my string.

37
00:02:17,470 --> 00:02:19,180
And it says, oh, look at that, there's an error.

38
00:02:19,180 --> 00:02:19,780
What's the error?

39
00:02:19,810 --> 00:02:26,050
Will the areas that changes in pointer down here, it actually expects to have a pointer to a string.

40
00:02:26,650 --> 00:02:33,160
And if I roll over this, it says cannot use my string type string as type string pointer, little asterisk

41
00:02:33,160 --> 00:02:34,390
and means it's a pointer.

42
00:02:34,400 --> 00:02:37,450
Well, what I need is a reference to that variable.

43
00:02:37,450 --> 00:02:45,790
All a pointer is a pointer is it points to a specific location in memory and gives you a means of getting

44
00:02:45,790 --> 00:02:47,860
that particular location in memory.

45
00:02:48,070 --> 00:02:52,810
Well, as you probably guessed, when you create a variable, you're creating a variable in memory and

46
00:02:52,810 --> 00:02:55,130
you're storing a value at a particular memory location.

47
00:02:55,690 --> 00:03:02,680
So what I need to do is instead of actually passing a parameter as a as an actual variable to this function,

48
00:03:02,890 --> 00:03:05,530
I want to pass a reference to that variable.

49
00:03:05,560 --> 00:03:10,570
I want to point to the location in memory where that variables contents exists.

50
00:03:10,840 --> 00:03:13,750
And I can do that by putting an ampersand in front of it.

51
00:03:14,140 --> 00:03:14,440
Oops.

52
00:03:14,470 --> 00:03:19,810
Get it in the right spot, put an ampersand in front of it and now it actually works.

53
00:03:20,620 --> 00:03:22,060
So I'm going to do one more thing.

54
00:03:22,480 --> 00:03:26,740
I'm going to copy this log print line and then I'll tell you why I'm going to all this trouble.

55
00:03:27,760 --> 00:03:29,230
I'll say after.

56
00:03:31,880 --> 00:03:38,030
Phone call, after I'm calling this function, my string is set to some value, so what do you think

57
00:03:38,030 --> 00:03:44,120
is going to happen here, bearing in mind that this function we created returns nothing and that the

58
00:03:44,120 --> 00:03:49,980
variable, my string up here exists only in the scope of the main function.

59
00:03:50,540 --> 00:03:56,330
So according to what I told you about SCOP, I really should not be able to effect this variable, my

60
00:03:56,330 --> 00:03:57,860
string down here.

61
00:03:57,860 --> 00:04:01,540
And I'm actually not trying to address that variable by name down here.

62
00:04:02,000 --> 00:04:07,160
And since this function returns, nothing, and I'm just calling it, I'm apparently not storing anything

63
00:04:07,160 --> 00:04:07,840
back in here.

64
00:04:07,850 --> 00:04:14,150
So what do you think might happen is to start the program, find the main function great, create a

65
00:04:14,150 --> 00:04:20,660
variable called my string store the value GREENAN It printed out my string is set to green and then

66
00:04:20,660 --> 00:04:23,240
I call this function that returns nothing.

67
00:04:24,020 --> 00:04:27,380
And then I print that again after function called my string is set to.

68
00:04:27,380 --> 00:04:28,640
You might think it would be green.

69
00:04:28,670 --> 00:04:31,700
Obviously it's not or it wouldn't be spending all this time talking about it.

70
00:04:32,060 --> 00:04:33,620
But look at what I am going to do.

71
00:04:33,620 --> 00:04:34,460
First of all, let's run it.

72
00:04:34,460 --> 00:04:35,240
So I'll run it.

73
00:04:36,200 --> 00:04:36,950
Look at the output.

74
00:04:37,820 --> 00:04:40,340
My string is set to green after function.

75
00:04:40,340 --> 00:04:42,370
Call my string set to read.

76
00:04:42,680 --> 00:04:46,100
So all I did in here was I took the pointer.

77
00:04:46,310 --> 00:04:51,590
So here I said pass a reference to this variable, to this function.

78
00:04:52,060 --> 00:04:54,770
Then this function says, OK, I'm now storing.

79
00:04:54,770 --> 00:04:58,520
The value of that is nothing more than a reference to this variable in memory.

80
00:04:58,520 --> 00:05:08,760
And if I come in here, you show this input logged print line and I say X is set to and I just print

81
00:05:08,760 --> 00:05:09,260
s out.

82
00:05:10,100 --> 00:05:10,880
What's it going to print?

83
00:05:11,120 --> 00:05:12,140
Well, let's see what it does.

84
00:05:13,520 --> 00:05:19,710
Go Roumain Go s is set to and then we have this hexadecimal number.

85
00:05:20,360 --> 00:05:21,460
Well what the heck is that.

86
00:05:21,590 --> 00:05:27,080
That is nothing more than pointing to a specific location on my computer in my computer's ram.

87
00:05:27,080 --> 00:05:31,310
It's memory and saying X is set to whatever you find at this memory address.

88
00:05:31,880 --> 00:05:38,120
So now that I have that memory address, I'm creating the new string called New Value, storing the

89
00:05:38,120 --> 00:05:38,870
value of reading it.

90
00:05:38,880 --> 00:05:45,110
I'm saying go to that memory address that you received up here and change its contents from whatever

91
00:05:45,110 --> 00:05:47,120
they happen to be to this value.

92
00:05:47,790 --> 00:05:54,710
So what I've effectively done is changed a locally scoped variable, a function, a variable that scope

93
00:05:54,710 --> 00:05:55,760
to the main function.

94
00:05:56,060 --> 00:06:01,840
I changed its value by passing a reference or a pointer to that variable, to some function.

95
00:06:02,180 --> 00:06:03,300
So that's what pointers are.

96
00:06:03,740 --> 00:06:09,830
So when you need to get the reference to a variable, you put an ampersand in front of it.

97
00:06:10,070 --> 00:06:14,300
When you need to refer to an actual pointer, you put an asterisk in front of it.

98
00:06:14,780 --> 00:06:17,510
Now, this will become very useful to us as time goes on.

99
00:06:17,840 --> 00:06:21,890
I'm only introducing it to you now because you may never have heard of pointers before and you're going

100
00:06:21,890 --> 00:06:24,050
to have to at some point in the not too distant future.

101
00:06:24,350 --> 00:06:29,540
They're a little confusing at first, but the thing to remember is a reference to a variable.

102
00:06:29,690 --> 00:06:36,110
Put an ampersand in front of it to actually create a pointer type, put it, put an asterisk in front

103
00:06:36,110 --> 00:06:36,290
of it.

104
00:06:36,290 --> 00:06:42,350
So ampersands for reference asterisks for the actual pointer itself when you're pointing to a location

105
00:06:42,350 --> 00:06:42,800
in memory.

106
00:06:42,920 --> 00:06:43,330
All right.

107
00:06:43,610 --> 00:06:44,930
So just keep that in mind.

108
00:06:45,530 --> 00:06:48,560
The best way to learn these things is to actually use them.

109
00:06:48,560 --> 00:06:52,490
And when we start using this, it will make more sense if it doesn't make sense to you now.
