﻿1
00:00:00,470 --> 00:00:03,770
‫So in this video, we're going to talk about pointers.

2
00:00:04,100 --> 00:00:09,890
‫And this is a topic that we're going to see a lot in the data structure section of this course.

3
00:00:09,890 --> 00:00:13,100
‫So it's really an important topic to understand.

4
00:00:13,580 --> 00:00:19,910
‫And to explain this, I'm going to start by showing something that does not use a pointer.

5
00:00:20,510 --> 00:00:23,960
‫I'm going to create an integer that we're going to call NUM one.

6
00:00:23,960 --> 00:00:30,650
‫We'll set that to be equal to 11, and then we'll have another integer that we call NUM two and we're

7
00:00:30,650 --> 00:00:34,100
‫going to set that to be equal to NUM one.

8
00:00:34,610 --> 00:00:43,550
‫So in this situation, when we set NUM two to be equal to NUM one, the thing that we're doing is passing

9
00:00:43,550 --> 00:00:46,460
‫the value down to NUM two.

10
00:00:46,490 --> 00:00:50,510
‫This is how it works when we are not using a pointer.

11
00:00:50,960 --> 00:00:54,890
‫So now let's flip over to VTS code and take a look at this.

12
00:00:55,670 --> 00:00:59,310
‫So these are the two lines that we just wrote here.

13
00:00:59,330 --> 00:01:06,320
‫And then with these two lines, we're just going to print out num one and num two and I'll run this.

14
00:01:07,350 --> 00:01:10,530
‫And you can see that they are both equal to 11.

15
00:01:11,100 --> 00:01:19,050
‫Now, if I come up here and add a line and I set NUM one to be equal to 22 and I run this.

16
00:01:20,500 --> 00:01:24,980
‫Num one becomes 22, but number two stays 11.

17
00:01:25,000 --> 00:01:28,870
‫This is how it works when you are not using a pointer.

18
00:01:29,230 --> 00:01:31,270
‫So now let's flip back.

19
00:01:31,720 --> 00:01:39,340
‫So number one is now 22 and NUM two is not changed, it's still equal to 11.

20
00:01:39,850 --> 00:01:45,370
‫So now that we've seen how this works, when we are not using a pointer, let's look at how it works.

21
00:01:45,370 --> 00:01:50,140
‫When we are using a pointer, we'll say int star.

22
00:01:50,140 --> 00:01:52,540
‫And the key here is that we have that star there.

23
00:01:52,540 --> 00:02:01,030
‫It means that NUM one is now a pointer and when we set this to be equal to 11, we do it like this.

24
00:02:01,450 --> 00:02:10,540
‫This creates a new integer somewhere in memory that has a value of 11 and we are pointing num one to

25
00:02:10,540 --> 00:02:12,850
‫that integer that we've created.

26
00:02:13,270 --> 00:02:17,980
‫Then we'll say num two is equal to num one.

27
00:02:17,980 --> 00:02:24,760
‫But once again we're using this star which makes NUM to a pointer as well.

28
00:02:25,570 --> 00:02:26,350
‫So numb.

29
00:02:26,350 --> 00:02:35,020
‫One is pointing to this integer in memory and when we set num two to be equal to num one, what we are

30
00:02:35,020 --> 00:02:44,830
‫not doing is passing this value down to num to what we are doing is passing the reference, the pointer

31
00:02:44,830 --> 00:02:53,320
‫and it makes it where num two is pointing to the same integer in memory that num one is pointing to.

32
00:02:53,800 --> 00:02:58,060
‫So now let's flip over to vs code and take a look at this.

33
00:02:58,630 --> 00:03:02,200
‫And that is where we created NUM one and NUM two.

34
00:03:02,200 --> 00:03:06,700
‫And with the C out statements, we will print both of these out.

35
00:03:07,210 --> 00:03:10,150
‫What we're going to get a very different output here.

36
00:03:10,150 --> 00:03:12,670
‫So watch what happens when I run this.

37
00:03:13,710 --> 00:03:20,520
‫Numb one and numb two are each equal to an address in memory.

38
00:03:20,790 --> 00:03:24,300
‫Remember when we set num two to be equal to numb one?

39
00:03:24,300 --> 00:03:26,460
‫We are passing a reference.

40
00:03:26,460 --> 00:03:28,620
‫That's what both of these are.

41
00:03:29,190 --> 00:03:38,040
‫But if you come over here and add a star here and a star here, what it'll print out is the integer

42
00:03:38,070 --> 00:03:40,500
‫at that address in memory.

43
00:03:40,980 --> 00:03:42,690
‫So I'm going to run this again.

44
00:03:43,610 --> 00:03:50,780
‫And you can see that now it is showing that num one and num two are each equal to 11.

45
00:03:51,320 --> 00:03:53,030
‫So now I'm going to come up here.

46
00:03:53,840 --> 00:04:00,500
‫And I'm going to paste this in setting that integer that num one points to to 22.

47
00:04:01,010 --> 00:04:08,750
‫And now when I run this num one and numb two are both equal to 22.

48
00:04:09,430 --> 00:04:15,430
‫And this is different behavior than what we saw before when we weren't using a pointer.

49
00:04:15,850 --> 00:04:22,120
‫When we change that integer, that num one points to it's going to change the integer that num two points

50
00:04:22,120 --> 00:04:25,000
‫to because it is the same integer.

51
00:04:25,740 --> 00:04:28,530
‫So now let's flip back.

52
00:04:29,100 --> 00:04:36,540
‫So numb one and numb two are now both pointing to an integer with a value of 22.

53
00:04:37,080 --> 00:04:39,660
‫So why is this important?

54
00:04:39,900 --> 00:04:45,960
‫The reason is when we get to data structures like a linked list, lists say this 22 is actually a node

55
00:04:45,960 --> 00:04:47,370
‫like this.

56
00:04:47,820 --> 00:04:53,010
‫The first node in the link list is going to have a pointer to it called Head.

57
00:04:53,340 --> 00:04:59,430
‫It's important to understand that head is a variable that is a pointer to a node.

58
00:04:59,850 --> 00:05:06,930
‫And if we create another variable called temp and we set it equal to head, what we're doing is having

59
00:05:06,930 --> 00:05:12,030
‫it point to the exact same node that head is pointing to.

60
00:05:12,480 --> 00:05:15,510
‫And just like head and temp are pointers.

61
00:05:16,170 --> 00:05:21,720
‫This is a pointer as well that can point to a node.

62
00:05:22,320 --> 00:05:26,430
‫So that pointer that goes from the T2 node to the Forfour node.

63
00:05:26,460 --> 00:05:34,350
‫If we were to set head to be equal to that pointer, it would point head at this node.

64
00:05:34,920 --> 00:05:40,620
‫And these are all concepts that we're going to use a lot and the data structure section.

65
00:05:41,100 --> 00:05:45,510
‫So let's take this 22 and turn it back into an integer like this.

66
00:05:45,870 --> 00:05:51,630
‫So I want to emphasize the point that we can take these pointers that will be pointing to some object

67
00:05:51,630 --> 00:05:58,650
‫in memory and point them at another object and memory that these pointers are not permanent.

68
00:05:59,160 --> 00:06:04,920
‫So I'm going to create another pointer to an integer, call it num three.

69
00:06:05,430 --> 00:06:12,150
‫And if we set NUM two to be equal to NUM three, it will just take num two and point it down here so

70
00:06:12,150 --> 00:06:14,550
‫these can all be redirected.

71
00:06:15,150 --> 00:06:18,360
‫And that is our quick overview.

72
00:06:19,230 --> 00:06:20,220
‫Of pointers.

