Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,180 --> 00:00:01,920
Welcome back, ladies and gentlemen.
2
00:00:02,070 --> 00:00:08,040
And now once you are done starting the basics, let's say, or the reason why you should even consider
3
00:00:08,040 --> 00:00:14,310
using pointers in this video, we're going to talk about how to declare pointers and how to use pointers,
4
00:00:14,610 --> 00:00:19,650
how it looks like when it's done in our C programming language.
5
00:00:20,100 --> 00:00:25,620
So we've seen previously that although the changes between the values that we've made in this WAB function
6
00:00:25,620 --> 00:00:31,380
when we passed by value did not have any effect on the variables in the main function.
7
00:00:31,680 --> 00:00:38,580
So this approach didn't work and that means we need some way to set up some sort of a connection, a
8
00:00:38,580 --> 00:00:45,750
reference between the actual variables we would like them to to get modified in the main function for,
9
00:00:45,750 --> 00:00:48,450
for example, are A and B variables.
10
00:00:48,570 --> 00:00:52,580
And we want that the function, this wolf function should should accomplish that.
11
00:00:52,690 --> 00:00:59,370
Okay, we want that the swap function will take both of these A and B variables somehow some reference
12
00:00:59,370 --> 00:00:59,760
to them.
13
00:01:00,180 --> 00:01:05,790
And then once it's done executing, they will be simply swapped.
14
00:01:05,910 --> 00:01:11,870
So we need a way to let this world function to access the actual variables in the main function, do
15
00:01:11,870 --> 00:01:13,380
the whole swapping operation.
16
00:01:13,740 --> 00:01:19,410
And after it's done, we will see the variables in the main actually swapped as expected.
17
00:01:19,860 --> 00:01:26,370
And we said that it can be done through some sort of connection, some reference between the boxes residing
18
00:01:26,400 --> 00:01:33,510
in one room to let these boxes to be accessed from another room just by using addresses.
19
00:01:33,560 --> 00:01:33,820
Okay.
20
00:01:34,260 --> 00:01:41,910
Just passing some actual coordinates of these variables, which in our case are the addresses.
21
00:01:42,060 --> 00:01:43,900
And for that to do that.
22
00:01:43,920 --> 00:01:48,540
To accomplish these tasks, we need to use our new friend called Pointers.
23
00:01:48,840 --> 00:01:53,640
So now let's introduce how pointers are being used in our programming language.
24
00:01:54,660 --> 00:02:01,680
And just a quick reminder, we can say that a standard variable, for example, in double char and so
25
00:02:01,680 --> 00:02:06,260
on, is basically used to store some standard value.
26
00:02:06,280 --> 00:02:06,480
Okay.
27
00:02:06,570 --> 00:02:12,780
We call it this way standard variable, which we learned in the previous section, sections, stores,
28
00:02:12,870 --> 00:02:14,840
some standard value.
29
00:02:14,970 --> 00:02:21,930
So we may be using it to store integers char to store characteris and you've got the a.D.A basically.
30
00:02:22,230 --> 00:02:26,510
So standard variable is used to store standard value.
31
00:02:26,640 --> 00:02:33,560
And we can say for now that a pointer variable is of some unique type which contains instead of the
32
00:02:33,570 --> 00:02:36,880
standard value as we are used previously.
33
00:02:37,200 --> 00:02:42,600
It actually contains the address, it has the address of where it points to.
34
00:02:42,780 --> 00:02:50,490
So for this Lavalin to make it more clearer to you guys, a pointer variable basically holds the address
35
00:02:50,580 --> 00:02:52,430
of some standard variable.
36
00:02:52,450 --> 00:02:52,680
Okay.
37
00:02:53,070 --> 00:02:54,560
So how can we declare it?
38
00:02:54,720 --> 00:02:58,440
How can we create our nice little pointer variable.
39
00:02:58,710 --> 00:03:02,900
So it will look basically like this and star B.
40
00:03:03,000 --> 00:03:03,410
OK.
41
00:03:03,720 --> 00:03:11,760
inStar P and int we presents the data type actually represents the type of the data we will be pointing
42
00:03:11,760 --> 00:03:14,220
to through these variable.
43
00:03:14,250 --> 00:03:16,830
So in this case, we will point.
44
00:03:17,010 --> 00:03:17,780
We will point.
45
00:03:17,790 --> 00:03:21,990
We will hold the address where resides and end a variable.
46
00:03:22,140 --> 00:03:30,630
So we will point to end and basically said that Pointer B points to an integer variable and simply states
47
00:03:30,720 --> 00:03:36,120
that three, the value in the address you are pointing to as an integer.
48
00:03:36,150 --> 00:03:43,620
So that's an important note for more advanced sections, although for a four for these course it's also
49
00:03:43,620 --> 00:03:43,930
OK.
50
00:03:44,040 --> 00:03:47,700
But it will be very useful for advanced courses as well.
51
00:03:47,820 --> 00:03:48,260
All right.
52
00:03:48,270 --> 00:03:51,780
So now we we can take a look at this new star.
53
00:03:51,810 --> 00:03:54,570
OK, we we didn't see it previously.
54
00:03:54,870 --> 00:04:01,100
And this star indicates that we are not using just the standard variable like we did.
55
00:04:01,260 --> 00:04:02,790
We've done it with integers.
56
00:04:04,050 --> 00:04:07,350
But we are working with some special.
57
00:04:07,350 --> 00:04:07,590
OK.
58
00:04:07,640 --> 00:04:08,940
Unique pointer.
59
00:04:09,360 --> 00:04:10,750
Let's call it this way, OK?
60
00:04:10,950 --> 00:04:13,080
And also we have lastly our P.
61
00:04:13,140 --> 00:04:13,570
OK.
62
00:04:13,620 --> 00:04:19,680
And P basically is just the name that we are giving to our pointer variable.
63
00:04:19,950 --> 00:04:26,760
So basically we just created a variable called P that is a pointer of type integer.
64
00:04:26,760 --> 00:04:33,150
It points to an integer variable to some either integer variable that we will assign it.
65
00:04:33,190 --> 00:04:34,680
But but that's later on.
66
00:04:34,680 --> 00:04:36,270
It's not in the declaration phase.
67
00:04:36,390 --> 00:04:42,090
So to summarize, we have some rule to declare a pointer variable and it goes like this.
68
00:04:42,330 --> 00:04:48,720
First of all, you specify the data type of there, the value that you want to that you know, you will
69
00:04:48,720 --> 00:04:49,860
be pointing to.
70
00:04:50,280 --> 00:04:57,450
Then you specify the star to to to say that it's of type pointer, four pointer type.
71
00:04:57,450 --> 00:04:59,910
And then you specify as previously the very.
72
00:04:59,970 --> 00:05:00,450
No name.
73
00:05:00,700 --> 00:05:01,660
So that's it.
74
00:05:01,780 --> 00:05:07,170
That's how you declare how you create a pointer in C programming language.
75
00:05:07,330 --> 00:05:14,290
So now we can take a look at some other examples and see what what what will happen behind the scene,
76
00:05:14,290 --> 00:05:19,570
which created a variable being the same box, similar box to what we've done.
77
00:05:19,750 --> 00:05:22,780
We've used previously B, the name of the box.
78
00:05:22,780 --> 00:05:27,310
It's just it's sticker and it says, I will hold the address, OK, later on.
79
00:05:27,640 --> 00:05:32,650
Once we use some assignment operation, I will hold the end address.
80
00:05:32,980 --> 00:05:36,140
And in this address that I will point to there.
81
00:05:36,160 --> 00:05:43,260
I expect to see an integer variable and similarly can be done for double the BDD.
82
00:05:43,300 --> 00:05:44,080
Just the name.
83
00:05:44,350 --> 00:05:49,930
And it says I will point to some double and char b c, OK.
84
00:05:49,960 --> 00:05:54,160
We basically can point also to character variables.
85
00:05:54,220 --> 00:05:54,470
OK.
86
00:05:54,760 --> 00:05:57,190
That's nothing are nothing new.
87
00:05:57,910 --> 00:06:02,560
Basically just to understand the pointers declaration process.
88
00:06:02,740 --> 00:06:07,540
So there are basically two main things you should keep in mind when using pointers.
89
00:06:07,960 --> 00:06:13,570
The first thing is that you should keep in mind the new R address ampersand.
90
00:06:13,600 --> 00:06:14,080
We've used it.
91
00:06:14,350 --> 00:06:16,150
We've used it in this kind of functions.
92
00:06:16,180 --> 00:06:21,190
But this ampersand simply specifies the address of a variable.
93
00:06:21,280 --> 00:06:24,180
And we know that every variable has an address.
94
00:06:24,190 --> 00:06:24,550
Right.
95
00:06:24,910 --> 00:06:26,680
And P is a pointer.
96
00:06:26,710 --> 00:06:26,990
OK.
97
00:06:27,060 --> 00:06:31,930
If we have B, we created B is a pointer to an integer variable.
98
00:06:32,410 --> 00:06:38,320
Thus we can say that p p we expect that it will have in the future when we make some assignment.
99
00:06:38,620 --> 00:06:45,650
It will have the address of some other variable, for example, are a, b, num, whatever it was previous
100
00:06:45,690 --> 00:06:50,140
location that it will have the address where it should, it should point to.
101
00:06:50,160 --> 00:06:51,940
So remember these ampersand.
102
00:06:52,240 --> 00:06:54,030
And remember the declaration state.
103
00:06:54,140 --> 00:06:56,200
And if we take a look at this example.
104
00:06:56,230 --> 00:06:56,530
OK.
105
00:06:56,620 --> 00:07:00,430
If we create a variable and initialize a variable a..
106
00:07:00,520 --> 00:07:03,220
Just like we've done previously in previous sections.
107
00:07:03,250 --> 00:07:10,240
So in a equals two five, we just created a standard variable like a box called a R.
108
00:07:10,300 --> 00:07:12,490
It has the value, a value of five.
109
00:07:12,520 --> 00:07:17,740
And also it has some label, some specification of what what its address.
110
00:07:17,770 --> 00:07:18,020
OK.
111
00:07:18,070 --> 00:07:20,110
So it has its own unique address.
112
00:07:20,280 --> 00:07:23,850
And now we can also create ar of the P variable.
113
00:07:23,910 --> 00:07:25,270
The pointer variable.
114
00:07:25,480 --> 00:07:27,200
Currently it's not initialized.
115
00:07:27,240 --> 00:07:28,980
It does not point anywhere.
116
00:07:29,320 --> 00:07:33,730
We should specify where these variables should point to.
117
00:07:33,820 --> 00:07:38,430
So now, just to make a quick reminder, if you want to bring the value of variable.
118
00:07:38,530 --> 00:07:41,250
You should simply use these print, print, off-line, ok.
119
00:07:41,410 --> 00:07:42,860
So print F percentage the.
120
00:07:43,310 --> 00:07:44,700
And the variable name.
121
00:07:44,710 --> 00:07:44,890
OK.
122
00:07:44,950 --> 00:07:46,690
It will print five to the screen.
123
00:07:46,810 --> 00:07:54,550
And if you wanted to print the address of these variable of variable a the actual memory where it resides.
124
00:07:55,150 --> 00:08:02,620
You should use print F percentage B and percent of a percent A specifies the address of A.
125
00:08:02,920 --> 00:08:07,030
So four for if you run this example in your computer are the number.
126
00:08:07,030 --> 00:08:13,630
Of course it's probably going to be different because when the place is the address where a variable
127
00:08:13,630 --> 00:08:20,740
A resides and my computer may be different probably is going to be different than the address it resides
128
00:08:20,830 --> 00:08:21,850
in your computer.
129
00:08:22,210 --> 00:08:29,170
So this number that will be printed out or whatever format you're using specifies the address where
130
00:08:29,200 --> 00:08:30,220
A resides.
131
00:08:30,280 --> 00:08:33,160
So we can print the value of a variable A.
132
00:08:33,490 --> 00:08:39,310
And also we can print and access and specify the address of variable H.
133
00:08:39,460 --> 00:08:45,800
So please type down, write down these lines of code by yourself and check out how it works for you.
134
00:08:45,940 --> 00:08:46,220
OK.
135
00:08:46,810 --> 00:08:53,560
Make megaliths practice even if it's a little bit R now, but we'll practice it in the next challenges
136
00:08:53,560 --> 00:08:54,700
and in the next videos.
137
00:08:54,970 --> 00:08:57,940
But still try to also parse this video.
138
00:08:58,190 --> 00:08:58,430
Right.
139
00:08:58,480 --> 00:09:03,790
This print F percentage, the R A and print are also the address.
140
00:09:03,820 --> 00:09:08,230
Just to feel it, just to understand what's going on behind the scenes.
141
00:09:08,290 --> 00:09:08,620
OK.
142
00:09:08,810 --> 00:09:11,590
And now once you're finished, let's get back to our example.
143
00:09:11,920 --> 00:09:18,470
So basically, we created we created an Int variable, OK, into a equals two five.
144
00:09:18,490 --> 00:09:20,440
That was initialized with some value.
145
00:09:20,740 --> 00:09:24,190
And we also created a pointer, a variable called P.
146
00:09:24,460 --> 00:09:29,710
We know that P is a pointer to end and it should point to any variable.
147
00:09:29,830 --> 00:09:35,890
And since we didn't assign any address for appeared to point to OK, we didn't specify where P should
148
00:09:35,890 --> 00:09:36,520
point to.
149
00:09:37,180 --> 00:09:41,110
We can say that for now it points to nowhere.
150
00:09:41,140 --> 00:09:41,410
OK.
151
00:09:41,480 --> 00:09:42,040
Nothing.
152
00:09:42,130 --> 00:09:43,300
Nothing to use for now.
153
00:09:43,780 --> 00:09:51,860
And so we want to basically two point OK to to specify that P will point to variable A.
154
00:09:51,940 --> 00:09:52,180
OK.
155
00:09:52,240 --> 00:09:59,810
So we want B to point to A and for now it may not be so easy to understand why we even.
156
00:09:59,940 --> 00:10:05,110
Want to do that and we'll put it in the logic, the main logic behind it.
157
00:10:05,350 --> 00:10:09,460
But trust me, guys, everything will become clear as we proceed with this section.
158
00:10:09,870 --> 00:10:18,850
And a lot of things will come when some time passes and you solve more and more exercises.
159
00:10:19,480 --> 00:10:23,410
So for now, just relax and understand the main concept of pointing.
160
00:10:23,890 --> 00:10:30,520
And what we want to do is to make be to point to a and we know that peace should hold and address of
161
00:10:30,520 --> 00:10:32,030
the variable it's pointing to.
162
00:10:32,050 --> 00:10:37,980
So basically what we are doing is specifying that P equals to the address of A..
163
00:10:38,080 --> 00:10:38,350
OK.
164
00:10:38,410 --> 00:10:40,540
So the logic is pretty simple.
165
00:10:40,540 --> 00:10:45,130
OK, we want B to hold the address of the variable it points to.
166
00:10:45,130 --> 00:10:49,630
So B equals two ampersand, A through the address of A..
167
00:10:49,720 --> 00:10:57,100
So now it will hold as its value of the address of Fey and visually it with it would seem that it simply
168
00:10:57,100 --> 00:10:58,000
points to a..
169
00:10:58,030 --> 00:11:06,190
So by using B we can access variable A since we know all of its coordinates and we can test it out by
170
00:11:06,190 --> 00:11:07,620
printing the address affair.
171
00:11:07,720 --> 00:11:07,910
OK.
172
00:11:07,990 --> 00:11:11,790
So let's bring the address of Fey and we are getting a triple threat.
173
00:11:11,860 --> 00:11:13,390
Try this one also on your own.
174
00:11:14,230 --> 00:11:17,470
And if you will, try also to print the value of.
175
00:11:17,920 --> 00:11:18,350
OK.
176
00:11:18,640 --> 00:11:25,270
You will see that both of them, both of them print the same result to the screen because, B, hold
177
00:11:25,360 --> 00:11:27,060
the address of a..
178
00:11:27,220 --> 00:11:27,590
OK.
179
00:11:28,150 --> 00:11:37,110
And basically, that's that's the main concept for four pointers is is it the result that you're running?
180
00:11:37,150 --> 00:11:37,400
Isn't.
181
00:11:37,680 --> 00:11:39,040
Is it the same guys?
182
00:11:39,100 --> 00:11:42,880
Is it the same for these two print off lines?
183
00:11:43,360 --> 00:11:46,030
Try try this one on your own practice, please.
184
00:11:46,120 --> 00:11:51,790
I, I, I'm asking you to feel how these line where you signed up.
185
00:11:52,270 --> 00:11:58,540
P equals two on person A, how it feels like and understand deeply what's going on here.
186
00:11:58,630 --> 00:12:04,510
So once you're done, we, we can move on to our last slide for this presentation.
187
00:12:04,530 --> 00:12:06,220
It's called the referencing.
188
00:12:06,820 --> 00:12:13,820
And we know that we can print the address of variable and that can be done using two ways, using our
189
00:12:13,990 --> 00:12:17,180
print F percentage P and the address of A.
190
00:12:17,650 --> 00:12:22,540
And also print P, the value of B, which is also the same.
191
00:12:22,660 --> 00:12:29,350
And also we said that we can print the values of variable A and that's pretty nice line.
192
00:12:29,350 --> 00:12:30,970
Will we know how to do it?
193
00:12:31,000 --> 00:12:34,240
We just access the A variable and print its value.
194
00:12:34,540 --> 00:12:37,600
But what I want to ask right now is how can we.
195
00:12:38,590 --> 00:12:42,100
How can we print the value of a how can we access it?
196
00:12:42,400 --> 00:12:51,340
Not by specifying the variable H itself, but rather accessing the variable A indirectly by using our
197
00:12:51,340 --> 00:12:57,350
pointer variable P that simply points to A and has all of its information.
198
00:12:57,400 --> 00:13:00,760
OK, because it can access its exact address.
199
00:13:00,880 --> 00:13:08,680
So if we want to print the value of the variable that our pointer points to, it can be done using these
200
00:13:08,770 --> 00:13:09,630
asterisks.
201
00:13:10,000 --> 00:13:12,160
This, this Stara that we've created.
202
00:13:12,460 --> 00:13:18,790
So what do we do with simply dereference a pointer to get the value that it points to?
203
00:13:18,910 --> 00:13:19,950
So it looks like this.
204
00:13:19,980 --> 00:13:21,690
So print F percentage the.
205
00:13:21,910 --> 00:13:25,810
And these points are psr p we are going.
206
00:13:26,260 --> 00:13:37,640
It can be translated and understood as we are going to the address that P points to and take these values
207
00:13:37,660 --> 00:13:38,870
that is in this address.
208
00:13:38,880 --> 00:13:41,020
And in this case it will be five.
209
00:13:41,050 --> 00:13:47,980
So these printed flying, these slants, last print off-line will also print the value of a which is
210
00:13:47,980 --> 00:13:48,430
five.
211
00:13:48,790 --> 00:13:53,940
So the last two print defines are basically, we can say kind of identical.
212
00:13:53,950 --> 00:13:56,350
They will print the same thing.
213
00:13:56,470 --> 00:14:04,580
So we are accessing the value of baulks or variable A indirectly using the P pointer.
214
00:14:04,720 --> 00:14:09,280
So that's also an important thing you should keep in mind when using pointers.
215
00:14:09,310 --> 00:14:11,740
This usage of of the asterisk.
216
00:14:11,800 --> 00:14:12,190
OK.
217
00:14:12,640 --> 00:14:17,710
And once again, the meaning of it is that let's say it once again.
218
00:14:17,740 --> 00:14:23,710
OK, go to the address that P points to and get the value that is stored there.
219
00:14:23,800 --> 00:14:25,140
So I hope that's clear.
220
00:14:25,240 --> 00:14:28,510
And with practice, you will feel much more confident.
221
00:14:28,930 --> 00:14:35,320
And don't worry, we will cover up this topic and a lot of topics that will follow.
222
00:14:35,770 --> 00:14:38,930
And also in advanced courses, we will talk about pointers.
223
00:14:38,970 --> 00:14:40,660
And so don't worry.
224
00:14:40,750 --> 00:14:44,930
You will understand pointers to their full list.
225
00:14:45,010 --> 00:14:46,360
Maybe not nothing.
226
00:14:46,690 --> 00:14:53,680
One hour of practicing, but with a lot of practice and a lot of hard work and a lot of lectures, we
227
00:14:53,680 --> 00:14:54,670
will make it.
228
00:14:55,420 --> 00:14:56,120
All right.
229
00:14:56,230 --> 00:14:57,650
So thank you guys for watching.
230
00:14:57,790 --> 00:14:59,270
And good luck.
231
00:14:59,320 --> 00:14:59,770
LCN.
232
00:14:59,930 --> 00:15:00,710
Next videos.
21233
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.