Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,210 --> 00:00:00,990
Welcome back.
2
00:00:01,020 --> 00:00:08,190
Ladies and gentlemen, to one of your most important and influencing topics in all of your programming
3
00:00:08,190 --> 00:00:09,000
careers.
4
00:00:09,530 --> 00:00:15,720
And in these section, as well as if you decide to study more advanced topics, pointers is something
5
00:00:15,720 --> 00:00:22,650
that will definitely accompany you because it's actually the bases and the core for so many things in
6
00:00:22,650 --> 00:00:28,980
programming as well as that's something that you're probably going to be asked on your C programming
7
00:00:28,980 --> 00:00:30,330
interview questions.
8
00:00:30,810 --> 00:00:38,400
So make sure you watch this topic from start till the end and practice that in the best possible way
9
00:00:38,400 --> 00:00:38,970
you can.
10
00:00:39,690 --> 00:00:40,750
So let's go.
11
00:00:40,980 --> 00:00:47,520
I want us to make a quick reminder of the swab code we've used in one of our previous sections.
12
00:00:47,790 --> 00:00:53,730
We simply wanted to take two variables into swap between their values and the main function.
13
00:00:53,730 --> 00:00:54,090
Right.
14
00:00:54,330 --> 00:00:56,190
So that was something like this.
15
00:00:56,220 --> 00:00:58,440
We created our main function.
16
00:00:58,770 --> 00:01:05,070
We initialized two variables into A equals two five, B equals two, seven and Vand.
17
00:01:05,070 --> 00:01:10,860
To implement this swap operation, we just created another variable called temp.
18
00:01:11,160 --> 00:01:14,730
We assign to temp A equals two, maybe equals 10.
19
00:01:14,880 --> 00:01:19,860
Basically all the swap functionality we implemented in the main function.
20
00:01:20,130 --> 00:01:22,740
It just looked like this.
21
00:01:22,890 --> 00:01:29,970
And now since we already know how functions can be used in what for they are being used, we would like
22
00:01:29,970 --> 00:01:36,420
to take this piece of code from the main function and put it inside some unique function and call these
23
00:01:36,420 --> 00:01:36,850
function.
24
00:01:36,960 --> 00:01:37,850
Just a swap.
25
00:01:37,890 --> 00:01:38,050
OK.
26
00:01:38,160 --> 00:01:44,190
Just to extract it out from the main function because we are already know how functions work.
27
00:01:44,340 --> 00:01:50,370
We know that these swap functionality between two variables may be used more than just once.
28
00:01:50,370 --> 00:01:50,750
Right.
29
00:01:51,120 --> 00:01:55,020
It's usage may be required in many places in our program.
30
00:01:55,260 --> 00:02:01,290
So that's why we would like to take these lines of code responsible for swapping and put them inside
31
00:02:01,380 --> 00:02:02,640
a separate function.
32
00:02:02,880 --> 00:02:09,330
So that now we will have all the responsibilities and all the code for the swap between two numbers
33
00:02:09,630 --> 00:02:14,850
in just one place and not scatter it in many places in the main function.
34
00:02:15,210 --> 00:02:21,540
And also, it will give us the ability to call this function whenever we would like to make some swap
35
00:02:21,540 --> 00:02:22,110
operation.
36
00:02:22,170 --> 00:02:27,630
Okay, so let's see how the code looks like in our C programming example.
37
00:02:27,750 --> 00:02:34,140
And basically we see that we have a function of a volume type, the name of the function, a swap.
38
00:02:34,620 --> 00:02:39,150
It has two values that it gets in one in two.
39
00:02:39,480 --> 00:02:43,290
So we whenever we will call this function, we will give it two values.
40
00:02:43,590 --> 00:02:47,180
One value will be assigned to number one and one value.
41
00:02:47,190 --> 00:02:49,890
The second value will be assigned to number two.
42
00:02:50,070 --> 00:02:56,460
Then what we are going to do is to create a temp variable that will assist us with on the swapping operation.
43
00:02:56,910 --> 00:03:03,890
And we will print out now one before this WAP and NUM, two before the swap made the swap operation
44
00:03:03,900 --> 00:03:10,380
the swap functionality and then print the values of number one and number two once again, just to make
45
00:03:10,380 --> 00:03:12,900
sure that the swap works as expected.
46
00:03:12,990 --> 00:03:18,450
So if we are to run this code right now, we will see on the console application that no one before
47
00:03:18,450 --> 00:03:21,300
swap equals to five and num two equals to seven.
48
00:03:21,530 --> 00:03:21,790
Right.
49
00:03:21,800 --> 00:03:25,860
As expected, because we initialize it in the main function.
50
00:03:26,310 --> 00:03:32,220
And also we will see that after the swap are we have number one equals to seven and number two equals
51
00:03:32,220 --> 00:03:32,760
two five.
52
00:03:33,030 --> 00:03:34,710
Everything works as expected.
53
00:03:34,800 --> 00:03:40,140
So we have this while function, this WAP room that whenever we call this function, it will receive
54
00:03:40,140 --> 00:03:44,880
two values and print them before the actual swap and after.
55
00:03:45,050 --> 00:03:48,660
And this function will print this information on.
56
00:03:48,780 --> 00:03:50,670
That's kind of a downside.
57
00:03:50,970 --> 00:03:55,630
It will bring these information every time, every time we're going to call this function.
58
00:03:55,980 --> 00:04:00,600
Are these two lines two four lines of this?
59
00:04:00,600 --> 00:04:03,420
What are the values before and after will be printed?
60
00:04:03,820 --> 00:04:07,830
And that's basically maybe something that we don't want.
61
00:04:08,130 --> 00:04:14,910
Maybe you will want this one function just to swap between the values without printing them and to leave
62
00:04:14,940 --> 00:04:18,210
maybe the printing onto another function and so on.
63
00:04:18,390 --> 00:04:24,810
So we can say that sometimes we may want it and sometimes we may not want these two lines.
64
00:04:24,870 --> 00:04:29,340
So let's just stick around with this one functionality for now.
65
00:04:29,370 --> 00:04:29,650
Okay.
66
00:04:29,940 --> 00:04:37,180
And let's see what will happen if we use these print lines in the function before and after.
67
00:04:37,200 --> 00:04:38,700
Calling this what function?
68
00:04:38,750 --> 00:04:38,920
Okay.
69
00:04:38,980 --> 00:04:40,050
We will see it right away.
70
00:04:40,320 --> 00:04:47,910
But basically, we we expect that the same result will will be seen on the screen that once on that,
71
00:04:47,910 --> 00:04:49,680
once the swap function is over.
72
00:04:50,010 --> 00:04:56,310
Then the variables in the main function will also be swapped and the messages will be seen on the screen.
73
00:04:56,670 --> 00:04:59,860
Miskelly, what we want to do is not to put the print line.
74
00:05:00,030 --> 00:05:05,850
The swap function, but rather to keep the printing, for example, in the main function, okay.
75
00:05:06,150 --> 00:05:09,590
And swap function will be responsible for just swapping.
76
00:05:09,680 --> 00:05:13,010
So let's try to do this and it will look like this.
77
00:05:13,010 --> 00:05:15,050
Here is our void's swap.
78
00:05:15,530 --> 00:05:18,530
It's responsible for swapping between number one and two.
79
00:05:18,910 --> 00:05:19,610
And there you go.
80
00:05:19,610 --> 00:05:21,740
You have the void main function.
81
00:05:21,860 --> 00:05:25,520
So basically, it brings out the numbers before and after the swap.
82
00:05:25,970 --> 00:05:34,220
And here we can see the function call a swap for A and B, and we just pass the values of A and B and
83
00:05:34,280 --> 00:05:38,360
the are getting inside of number one and number two.
84
00:05:38,840 --> 00:05:41,790
And if we wrong this program right now.
85
00:05:41,840 --> 00:05:42,110
OK.
86
00:05:42,200 --> 00:05:45,920
And you may just copy and run it on your own to see what happens.
87
00:05:46,070 --> 00:05:48,710
You will see that nothing really happened.
88
00:05:48,770 --> 00:05:53,720
It seems that the variables A and B and B were not swapped.
89
00:05:54,200 --> 00:05:57,800
And that's a very important note that you must understand.
90
00:05:58,100 --> 00:06:06,080
So y y basically it's happening, guys, y, A and B remain the same even after the call to this swap
91
00:06:06,080 --> 00:06:06,710
function.
92
00:06:07,850 --> 00:06:09,380
If it's done correctly here.
93
00:06:09,400 --> 00:06:09,670
Right.
94
00:06:09,680 --> 00:06:13,250
It's done basically correctly y of the result.
95
00:06:13,280 --> 00:06:21,090
The printed A and B values seems to be the same, meaning A equals two five before and after the swap.
96
00:06:21,110 --> 00:06:22,610
And it shouldn't be like that.
97
00:06:22,700 --> 00:06:27,360
It should contain the value of B, which in this case is just seven after the swap.
98
00:06:27,420 --> 00:06:29,090
But but it's not what happens.
99
00:06:29,120 --> 00:06:29,360
OK.
100
00:06:29,540 --> 00:06:32,810
Try to run it on your own just to make sure that you see the problem here.
101
00:06:33,530 --> 00:06:41,030
So basically, although off for number one and number two, it seems that they were swapped inside the
102
00:06:41,030 --> 00:06:41,870
swap function.
103
00:06:41,900 --> 00:06:47,300
OK, so now one and two were swapped because they are local to the swap function.
104
00:06:47,480 --> 00:06:54,200
But the actual A and B values, which are in the main function, which are values that we want to swap,
105
00:06:54,520 --> 00:06:55,460
were Nance swap.
106
00:06:55,610 --> 00:06:58,280
That's very interesting and that's the basis.
107
00:06:59,540 --> 00:07:06,380
And one of the first and most important nodes explaining the reason of why you should consider using
108
00:07:06,380 --> 00:07:13,970
pointers as well as what is passing by value and passing by reference that we are going to talk right
109
00:07:14,000 --> 00:07:14,390
now.
110
00:07:14,510 --> 00:07:16,430
So I hope that you are ready.
111
00:07:16,520 --> 00:07:23,150
And in the next video, what we are going to do is that we are going to try and to visualize all of
112
00:07:23,150 --> 00:07:30,890
these situation of this swapping and thoroughly understand what happened here and why it seems that
113
00:07:30,950 --> 00:07:34,130
things were not working as expected.
114
00:07:34,400 --> 00:07:42,680
And also, we are going then to describe and to to learn why we should consider using loops.
115
00:07:43,130 --> 00:07:46,430
And when we should use it and how we should use it.
116
00:07:46,760 --> 00:07:47,870
So don't worry.
117
00:07:48,260 --> 00:07:50,270
This topic is not the easiest one.
118
00:07:50,330 --> 00:07:52,130
But we will cover it.
119
00:07:52,310 --> 00:07:53,270
I think pretty good.
120
00:07:53,330 --> 00:07:54,890
So I'll see in the next video.
11408
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.