Transcripts
1. 1 Introduction: Hello and welcome this
new course in which we are going to
chew pointers in C. In this course, you will
learn what are pointers, how they allow us to
optimize the program code. And you will learn
how to convert complex data types into
a sequence of bytes. You can develop programs
without using pointers, but they significantly speed up the execution of
the program code, which is especially important when working with
microcontrollers. And in this course, we will cover
everything about that. Most of you know that
pointers is one of the very important topics. For every embedded
system engineers. Anyone who program in C must use pointers or he will fail behind. A lot of interviews or
embedded system developers, interviews ask a lot of
questions about pointers. And the goal of this course
is to help you get started and learn how to use pointers and the different
types of pointers blast, covering a lot of
important topics. We will explain everything and we will give you
practical examples, assignments, and a lot of other resources to help
you master pointers in C. Now, let's say we're clock up what you will
learn in this course. You will learn indirect
addressing using pointers. Then we will cover different
types of pointers. After that, we will
talk about pointers, two main types, and
pointers to arrays. We will also talk about
pointers to functions. Pointers, the void, dynamic variables, and
pointers operations. And then we'll talk about datatypes, conversion
using pointers. And all of these topics
will be covered in details. Again, you will take
assignments so that we are sure that you've got
what we explained. We will also explain
practical examples. We will solve them together to give you a
hands-on experience. Thanks for watching this
introduction. See you inside.
2. 2 Introduction to Pointers in C: Hello and welcome to
this new lesson in which we are going to talk
about pointers. Pointer is a
variable that stores the memory address as h value. Things might not be
clear at this point. So let's first talk about the syntax for
creating a pointer. Then we will talk
about this definition. As you can see here, we have different syntax to
define a pointer. The first one is typing
into pointer type, adding the asterisk sign, then typing the pointer
name, adding a semicolon. The same thing happens here, but instead of
writing the asterisk close to the pointer name, we are writing it close
to the pointer type. The third syntax is the
same as the first two, but we are leaving space after and before
the asterisk sign. Now, in practice, you can see here that we have a
pointer called PTR. The type of this
pointer is integer. And to make this pointer, we must add the asterisk sign. Now, here we are adding it
right before the pointer name. Here we are adding it right
after the pointer type. And here we are adding
it in the middle between the pointer
name and the type. Now, as you can see here, there is no difference
in these three syntax. All define a pointer that
points to integer type, which is the main
goal of a pointer. Now, again, a pointer points. This is a pointer that points
to a location in memory. So again, the pointer
is a variable that stores the memory
address as its value. Now to make things more clear, this is an example. The address of operator is a unary operator
represented by the symbol. And now if you looked
here at this symbol, this is called address
of the and sign. It gives the address of any variable if it is preceded
the variable name. To make this more clear, if we wrote this and x, now this x means give
me the address of x. The address of x will
be stored inside PTR. And in this case we
have this memory. And this is the variable x. Now, the address of the
variable x is 1 thousand. Now, the value of x is
ten inside by memory, but the ad risk of x in
memory is 1 thousand. So in order to
reach this number, which is 1 thousand, we
need to write this and x. So PTR here will equal 1 thousand because
we use the unsigned, which means the address. So the address of operator is an operator that takes
only one argument. It's basically the and symbol. It gives the address
of any variable if it is preceded
the variable name. Now. And x equals ten, which is this. Now, x equals ten. It's a correct sentence. We are creating a new
variable called x. We are giving it a value of ten. So it will go to the memory, create a new variable
called x here, and it's giving it
a value of ten. Now int asterisk sign PTR, we are creating a pointer of
integer type called BT off. And here is that point
or that we created VTR. And the data type is integer. Now, the last step is adding an address
inside the pointer. To do that, we use this line, PTR equals address of x. Now an sign, again, it's called others off. We are going to find
the address of x, and we are going to store
the address of x inside BPR. So the address of x
here is 1 thousand. So we are storing 1 thousand inside BT are using this line. Now. Add this off PTR equals what? Can you think about it? We already mentioned that and
sine is the address of VTR, is now a variable in memory. It's a pointer, but it
has a location in memory. So this is all PTR
and the other, so VTR is 2000s, so t equals 2 thousand. So this sign is called
the address of sine. Now, keep in mind that that
pointer itself is a variable, has an address in data memory. Like any variable. The only difference
is that the pointer holds an address of
another variable. It does not hold regular data, it holds an address
of another variable. Now, Let's clear the screen so that we can finalize this. Now, here we defined
integer named x and initialize it
with value often. Then we defined a pointer
to int named VTR. After that, we initialized
PTR with the address of x. That way, we have
everything ready. Now, we can do some operations. First, we already defined
our variable named x, which type is integer, and gave it a value of ten. We defined pointer to n named
VTR and we initialized it. Now in this case, look
at this sentence. We have the asterisk sign, and we have the pointer name. And we have here equals. Now the dereference operator is a unary operator represented
by the symbol asterisk. This is called
dereference operator. And you need to
memorize these names we already mentioned and which
is the address of that. Now we have the asterisk sign, which is the
dereference operator. It say NOT operator, which means that it only
takes one variable. It operates on a pointer
variable and gives the ability to access the variable that
the pointer points to. Now things might not be
clear at this point, but we call it d
electrons because it takes away the
need for the address. And it allows you
to directly deal with the variable that
the pointer points to. Let's explain this
with an example. As you can see here, we
have x, we have VTR. Now PTR has the address of x, which is 1 thousand, and x has a value of 50. Now, we already know that
x has a value of ten. And in order to change it to 50, we are doing the following. The first thing that
we are doing here is we are writing that
dereference operator. And you can see it here. Then the pointer name. Now this means go to PTR and
check the address inside it, which is 1 thousand. And go to the variable
that has this address. So in this case, the
reference PTR means x, because 1 thousand is
pointing to x here, which is this variable. And we are telling it that we want to store 50 inside this. We are not storing it inside
PTR. Ptr does is this. We are storing it inside
that div reference of PTR, which is basically
the variable x. So x equals 50. That's why we remove ten. And now a new value is 50. Now let's clear the ANC
and explain it again. Now, x value is ten. When we use the
dereference operator, which is the asterisk sign, we are telling it to dereference
PTR, which is a pointer. Dereferencing a
pointer means going to the variable that a
pointer is pointing to. And in this case, the pointer is pointing with this
address to variable x. So by writing asterisk
or dereference PTR, we also telling it to go
to x and make it equal 50. Now let's explain it. If we have another variable, let's say that we have y here and we have 3 thousand here, which is the address. And we have another
PTR here between R1. And let's add the address
of y, 3 thousand. Now if you wrote it like that. Now let's make the value of 55. Let's add asterisk sign PT
R1 equals, let's say 15. Now this means dereference. Pt R1 means go to the variable or to go to
the pointer called BT R1. Get the address from it, and check the variable that this address is pointing to and the change
its value to 15. So the first step is going to the PT R1 pointer,
checking the address. After checking the address, going to the variable that
this address is pointing to, which in this case is y, and changing the
value of y to 15. So now y equals 15. Now to summarize things, the unsigned means
the address of PTR. When we add an asterisk
sign before the pointer, we are dereferencing
the pointer. So we are basically giving it the ability to access the variable that the
pointer is pointing to. And this is the main goal
of that dereference sign. Now, in the next lesson, we will solve a
practical example that I need you to try
to solve it on your own. I needed to create a program
that defines an integer and initialize it with
them, then print it. Afterwards, define a
pointer that points to that integer and
change the value of the integer through
the pointer to B20. Then print it again. Now the expected output is
x before change is ten. X after change is 20. Make sure to solve it before moving on to the next lesson. Thanks for watching this lesson.
3. 3 Solving First Practical Example: Hello and welcome to this
new lesson in which we are going to solve the
first question. You need to create a program
that defines an integer and initialize it with
ten, then print it. Afterwards. Define a pointer that
points to that interior and change the value of the n through the
pointers to B20. Then print it again. The output should be this. X is ten, then x after changing it using
pointer, it will be 20. To solve this question, all
use Visual Studio Code. Go to the Start Menu and
right visual studio code. Here it is. Now. You need to create a new
file or a new project. Now, file, new file. And here you need to click select a language and choose C. Now, we are good to go. The first step is including the std input output
string.h library, which is the main standard
input output library for our program. So I should include
STD. Order to etch. After doing that, you will
need to go to n. I mean, this will be our main function
that will be executed, will take nothing which is void. And the first step here is
to define a new integer. So x equals 0. Then print f. Let's ask the
user to enter the value. So please enter a number. Now, we need to scan
this number using scan f. The number that we will go out I'm going to scan will be a decimal number. So here, right. All of this was explained in
our C programming course. And there's no need
to explain again because we are teaching
pointers here. Now. We need to print again the previous value
and the new value. So x before change. X before change is
the percentage d. Now we need to change
x using pointers. So let's create a new pointer. Let's make it point
to the address of x. Now, to change the value of x, we will use the
dereference operator, which is the asterix PTR, and make it equal 20, which is the new value. And we will print the
same line, which is this. But we will say after change. Now, we have everything
set and ready. We can change this x with the reference PTR and
we'll plan the same thing. We can add here, this UTR and the other discipline
that this means x as well, because we are dereferencing
the address inside PTR, which is the address of x, so it will be the value of x. Now, notice the code. You can simply paste it
on the Notepad Plus, Plus, as we already
explained, and save it. Let's call it pointers example. Now we need to save it as
dot c. And let's run it. Now what IGCSE pointers,
example, Nazi. After that, write a dot EXE. And as you can see,
it's asking God, please enter a number.
Let's say five. Now, x before change is five, x after change is 20, which is exactly what
we have done here. We change the value
of x using pointers. That's it for this lesson.
Thanks for watching.
4. 4 Pass by value and Pass by address in C: Hello and welcome
to this new lesson in which we are going to explain pass by value
and pass by address. Now, in this lesson, we're going to discuss these two tabs of passing
the value and address. The first type is
passed by value. It means you are making
a copy in memory of the actual parameters
value that is passed in. A copy of the contents
of the actual parameter. While in past by address
or pass by reference. You are basically making a copy in memory of the address
of the actual parameter. Two are not making a copy
of the parameter value. We are making a copy in memory of the address
of the actual parameter. Now, let's take a look
at these examples. Again, pass by value will make a copy in memory of the
actual parameter while passed by address or
refinance will make a copy in memory of the address
of the actual parameter. So here we are making
a copy of the address. Here we're making a copy
of the actual parameter. Now, as you can see
in this example, here we have a function and this function is called
by value function. It takes integer x. Now this function
takes an argument by value because it takes
an actual value, which is basically a
variable x of type integer. Now in here, when we are
calling the function, we are passing the variable that by making a copy of
the actual parameter. We are only dealing here
with actual values, actual parameters, we are
not dealing with addresses. Now, when it comes to
pass by address or reference, as you can see, in this function, we have
a function called by others function and it
takes a pointer as input. So integer asterisk PTR, this is a pointer. Now, this function takes
an address as odd ones. So the input to this
function must be an address. And we are passing the
variable z by others here. We cannot add because it
is basically a value. And this function
wants an address. So here we added the and sign, which means the address of
it. As you can see in here. Now the address of z will be passed to the byte
address function. While here the value of z will
be pass by value function. So we are basically making a copy of the
actual parameter. While in here we are
making a copy in memory of the address of which is the address of the
actual parameter. And this is the main
difference between pass by value and
pass by address. There are different
use cases and you are going to take few
practical examples. But this is everything
you need to know for now about pass by value
and pass by address.
5. 6 Pointers increment and decrement: Hello and welcome
to this new lesson. We'll talk about
operations on pointers. Now. The first operation that we
are going to talk about is the increment and
decrement operations. The increment
operation increments the pointer value by one step. Now, we didn't say by one byte
because this step might be 148 or any number of bytes
is the size of the point t. Now, if we wrote PTR plus plus, if we assume that PTR is
a pointer to integer, then when we are
saying plus, plus, we are going one integer step, which is basically four bytes. Depending on your compiler. The compiler has
integer as four bytes. It means that every
increment will add four bytes to
the address of PTR. Now assuming that a PTR
address is 1 thousand, it means that we are now at 1004 because of
these four bytes. So each increment
or each pointer increments will increment the
pointer value by one step. Again, the stub is the
size of the pointy without integer or
string or character, or whatever type of data. Now when it comes
to the decrement, it's the same scenario. Decrements the pointer
value by one step. It's written like
that, PTR minus minus. And if we assume that
integer is four bytes, It means it will go
four steps back, it will become 996. And this is basically how increments and decrements works. Now let's see this in practice. Now let's start with
a simple example. Hash include stdio.h. Now int main, void. And let's select the
language to be c. Now, let's create a new
variable and x equals ten, and create a pointer. Now, PTR will hold
the address of x. And this is, these are
three simple sentences. Now, what we need to do is to
increment or decrement PTR. But first we need to print. So let's write print f PTR address, and we need to print the
address of the pointer. Now, add P as the
format specifier. And p N here stands for
pointer slash n. And after that you can add the
address of a ECS instance. You can add PTR, both works. Now we will add
the address of x. In order to prevent
the ad is correctly, we need to cast it as void. Now, let's run this to
see how things will go. So open containing folder, let's first save this file. Let's call it pointer element. Let's see. Now after that, Let's
combine this pointer. Let's use ls first, GCC pointer dot c. Now let's find a EXE. And here we have PTR
address is 01, F, 1 fourth. Now we can run that increment and decrement
to see how things will go. In here, you can say PTR plus
plus and PTR minus minus. Now you will note that if
we replaced this with PTR, Let's try to run the codes. As you can see, we are
getting the same results. Now. Let's print the same thing
after the increments, then print the same
thing after the colon. Now the increment will
increment the address by four, which is the number of
bytes and an integer. Then it will get it back by
four using the decrement. So let's see how things will go. Going back. Let's
combine using GCC. Let's run. Here it is. Now, as you can
see here, 61 E14. After we incremented by
four bytes, it's 18. Now, I decremented the value
again, PDR minus minus. So now it's back to one for
increased by four bytes, then decreased by four bars. So go to the original location. Again, PTR, increase and decrease depending
on the type of the point. Now we can change
it to character. Now if we go back here, Let's try to combine. Now run, as you can see, that actor is only one byte. Now it's at 17 by
incrementing it at 18. And by decrementing
again spat on 17. So as you can see, the increment and
decrement happens on bytes level depending
on the data type. And it's incrementing
and decrementing of the address. Not the value. Thanks for watching this lesson. If you have any questions, I'm here to help this.
6. 7 Pointers Addition and Subtraction: Hello and welcome
to this new lesson in which we're going to talk about addition and
subtraction on pointers. Now, to subtract the
value from pointer, Let's first define
a new pointer. So let's add x equals ten. And this creates a pointer. Let's point this pointer
to the address of x. Now, as in our previous example, we define these and we are going to do
operations on this. The first step is subtraction from pointer to
subtract value from pointer. Ptr equals PTR minus,
let's say four. Because we are dealing
with an integer. Here are assuming that
integer is four bytes. But you can subtract any value. Now once you execute
this command, if we assume that PTR
address is 1 thousand, now it will become 960 or 84, sorry, because we have four bytes and four
by four is 16 bit. So we went back 16 bit. Now this is when it
comes to the bit-level. Now to make things more
clear, let's print. Thanks. So print f, address, value, the percentage
be sewing and slash. Now here we want to
print the PTR value. We must cast it. And then we can add B2. Now to print the value after
we subtracted four from it. Let's go back and
execute this. So save. This is the name of our file. So let's copy the name. Now. Let's run this code. Now, compiler GCC E. And here we have it. As you can see here. The value before the
subtraction is if B14. Now the value after first
subtraction, subtraction is 04. And in here we went back. As you can see, we
subtracted four from this. Now, know exactly how
many steps we went back. Let's subtract one. Now save that code
again, go back, combined and run the code. Now as you can see,
when we subtracted one, we went from 14 to ten. We went back four steps. Now if we subtracted four, we will go back 16 step. Now, another thing
that you can do with pointers is subtract
pointer from pointer. So let's create another
variable. Let's call it y. And let's create
another pointer. Let's call this
one and this tool. And let's change
the values x and y. Beta1, beta2. Here we have PTR one, and here we have PTR C2. Now, what we need to
do is the following. We will create int x
equal R1 minus PTR to. Here we are assuming that
we have two pointers. And both of them are
pointers to integer, which is four bytes. Now PTR one minus beta two will return the number
of steps between them. So let's do this. And the last step is printing. The result is
percentage d slash n. And n. Here we need
to add x over x. Now, let's go back
to the command line. Now. We have problem here. X is defined again, so we need to make it. Now, as you can see in here, we have point or one others. We have pointers to address. And we subtracted pointer
one frame pointer tool. And we found out that the space between them is this number. Now when it comes to
the pointers addition, we can add a value to a pointer. To do that, we can
simply do the following. We can say PTR V1 equals
V2 R1 plus let's say four. And we can print it using the print command
that we all have used. R1, void B to R1. Okay? Now, we already know that we've got on others
will be printed here. So let's see how this
will affect the result. Now, edit EXP. And here we are. This is PT R1 address. And this is the
address after adding four to the PT R1 value. As you can see, it
moves from 08 to 18. And this is what we
are talking about. You can easily add or subtract values depending on
the type of operation. Now, the thing that's
not allowed is, so adding a pointer to
a pointer like the, like, subtracting them
in here is forbidden. It's not allowed, and it will
result in a compiler error. If you try to add point at
1 or two with a plus sign, you will get a
compilation error. So it's only allowed to add
a value to the pointer. It's not allowed to add
two pointers together. But you can subtract the
value from a pointer, or you can subtract two
pointers from each other. That's it for this lesson. If you have any questions please ask and if
you're on the board.
7. 8 Pointers and Arrays: Hello and welcome to this
new lesson in which we are going to talk about
arrays and functions. Now, the first thing that you need to know
is that the name of the array is the address of
the first element in it. So if we saved and ARR, which is an array that
has ten elements, then this is the
address of the SE, of the first element and the
array which is element 0. It's exactly like saying
at risk of a are all 0. These two are identical. So defining an array
using this method means that we are pointing to the address of the first
element in the array. Now, all elements of the array are saved sequentially
in the memory. Which means if you
have a pointer to the first element
in the array, incrementing the pointer
makes the point or points to the second element and keep incrementing it
to reach the third, fourth, fifth, and so on. Now, another good
thing to know is that dealing with the
array and the fact that animal data
is the address of its first element allows
us to do a lot of things using arrays by
using them as pointers. Again, ARR here is the
same as the address of 0. Which means if you
pass the array name ARR to the function called,
let's say function. So function. And we give it. Then in this case, we are passing address. We are not passing value. So this function
prototype should be declared as follow
void function. And in here we need to make sure that we are adding a pointer. We are not passing by value. We're passing pie Alice, even though we are writing out, we're not writing anything that indicates that
this is a pointer. But as we already mentioned, this is the name of our array. And the name of the
array is basically the address of the first
element in the array. And this things comes a lot, an MCQ questions and interviews and in lots
of different scenarios. Now inside the function, when we want to dereference PTR, which is the pointer. This means that we are accessing the first
element of the array. And if we did it, we incremented PTR by one. Then we are accessing the
second element of that array. B to R. Two is the third
element, and so on. You need to make sure to keep these notes in mind
when dealing with arrays inside C. Because arrays are very
much like pointers, and understanding pointers
will make dealing with array are a
much easier job. Now, another important note is that subscript TR and dereference operator
when dealing with pointers and arrays. Now, the subscript operator, which is these two signs, it was used with arrays when
we are defining a new array. It can also be used
with pointers. So let's say that
we have a pointer. So let's comment. Okay. Let's say that we
have a pointer, PTR. Now, when we say PTR 0, using this subscript operator, it means the same as saying,
beautiful dance detail. These two are identical. We are calling the first element or the first address
of the pointer. Now, when we want to access the second address after
the pointer address, we can simply write PTR one. And then this case
we are accessing the address next to
that first PTR address. And it's the same as
now PTR one here is the same as dereferencing
PTR blas one. And PTR ten, for instance, is the same as dereferencing
PTR who'd lost ten. And this is the main point here. We can use the subscript two. Operator with pointers, just like arrays to reach or
find an exact element. Now, to make things even
more clear, why not? We try this with a
practical example. Let's remove all of this. Now. First, I will define a function. And as most of you know, to define a function, first, we need to call the prototype
or initiate a prototype. And then we need to implement the function, the
function implementation. Then we need to call it. So let's first start with the prototype.
Let's call it fun. And in here and asterisk PTR. So this function will take PTR or pointer as input
and to return nothing. Now, let's find the
function implementation. Remove this, add
two curly braces. In here. We need to add a for loop. Inside this for loop
will loop from 0 to, let's say, ten I plus plus. And in here, we
need to add print f. And in here we need
to print the values. And we would be too hard. But instead of
fighting PTR alone, we will use the subscript TR and add just like with arrays. After doing that, we
can call the function. Now here comes the trick. When we are calling
the function, we are not passing, let's say a value, we are passing an address because as you can see in
the function implementation, we are receiving a pointer. Now let's create an array
and ARR then equal, and let's give it ten values. So 12345678910. Now, in order to pass an
address to this function, we can do that using the array. As we already mentioned. We already mentioned that
the array or array of ten is basically the same way as calling the first
element inside that array. So in order to call the
first element address, we need to pass it using ARR without writing
anything else. Now, this will make
sure that the funk will receive the address
of the first element. And it will increase
this address by one using PTR
subscript right here. So it will loop from
the first element to the last element and
see this in action. Let's save this and go back. Now, GCC arrays, pointers. Let's see. Now as you can see, we have an issue here, which is the fact that
I is not defined. So let's define it. Again. Now. As you can see, we just
printed the array without having to pass the
value of this array. 12345678910. And we only pass the address of the first
element of that array. We use pointer subscript
operator to loop through this array by simply moving
from one address to the next, to the next to the next
by increasing one, the PTR using that diesel
or the subscript operator. I know that things might be a little bit confusing
at this point, but with this example, you need to take a quick
look at this example again. Try to watch this lesson
more than once in order to digest the
information inside it. Now, let's do a quick summary. The name of the array is the address of its
first element. So if we have this array, then ARR here is the
address of array 0. Now, if you pass the array name or like in this case
to this function, then you are passing
address to end. The function.
Prototype should be declared with a
pointer as inputs. Now, inside the function func, when we dereference PTR, then we are accessing the
first element of the array. And we can access
the first element by either writing PDR, last one to access
the second element. Or we can simply use PTR. And here we are using
subscript or for I. And we are starting with the
first element addressed. Then the second element, the third element adds. And I'm saying here, I'd just, because we are dealing
with addresses, we are not dealing with values, because we just
received the address of the first element and we are looping through
the addresses. And we are printing
the value that these addresses are pointing to. That's it. This is not
an easy example, I know. But again, operators
or let's say pointers are not an
easy thing to learn. That's why you need to
watch this lesson more than once in order to digest
the information inside it. If you have any questions or if anything is not clear
in this lesson, please ask and the mandible, I'll be more than happy to help. Thanks for watching.
8. 9 Dangling Wild Void and Null Pointers: Hello and welcome
to this new lesson in which we are going to talk about dangling pointer
and wild pointers. A dangling pointer points to a memory address which
used to hold a valuable. Since the address it points
at is no longer preserved, using it will lead to
an expected result. So in order to see
this in action. Now, in here, the first
step is creating a pointer. So n asterix PTR. Now what we need to do is
to use the malloc function. This malloc function
will return void. So we need to cast to
an integer asterisk, which is the pointer
to integer to convert void pointer
into integer pointer. So go back here
and write integer, which is basically
an integer pointer. Now proceed with the
model and we need to call the size of function. And we will solve the size
of integer inside here. After doing that, we
will create a variable, call it a, and
make it equal 560. Then we need to make the point or points
to the address of a. Then we will use
that free function. The function will simply
deallocate the memory. So right, be PTR. That's it. Now, we did free that pointer. We already mentioned that the
dangling pointer points to a memory address which is
used to hold the variable. Since the address it points
at is no longer reserved, using it will lead to
unexpected results. Now, to show you the
unexpected result, we can do the following. We did the allocate the PTR. So we can write PTR equal five to store five inside that dereference
VTR, which is a. And the last step is printing. So print f. And in here we will use percentage d to plan
the reference of PTR. Now let's save the code. And here we are testing
on dangling pointer. So go back, CLS, GCC. Okay, Now this you see
dangling pointers c, k. Now we have a problem. We forgot to include a CD live. So let's include it. Saved the code, and go back. Again. Here we are. Now as
you can see here, we did compile this code. Now, let's run it a EXE. And as you can see,
it's blinking. Now we haven't
received anything. It should have printed that
different fronts of PTR. But as we already mentioned,
we deallocated PTR. So this is a dangling
pointer and using it after the allocating it will result in
unexpected results. This is what a
dangling pointer is. Again, a dangling
pointer is a pointer that points to delete it
or the allocated objects. And you need to
make sure that you don't trace such scenarios. And try to avoid them. Try not to play with pointers. They allocate them or delete them without knowing
what you are doing. Now the second type of
pointers is the wild pointers. Now, let's comment this part. While pointer is a pointer that is used before initialisation. An example of that
is this end PTR. If we use the print
f statement and try to print PTR using
the dereference PTR. We haven't yet initialize
the PTR with an address. So this is called
a while pointer. Now save your codes, go back and combine, then run. As you can see, again, an unexpected result because
this is our pointer. We haven't initialized
this pointer. And that's why we
are facing this. Now. No arrows drawn, but we haven't received
any results because. This pointer was not
initialized to begin with. Now how to turn a valid
pointer into another? So while pointer, It's easy. As you can see, we already created the pointer, so we need to make it
point at something. We can initialize it by simply
creating another integer, call it, give it a value of ten. Then we will say that PTR
will equal the address of x. That's it. And now we can execute
that code again. As you can see, we got five, which is the value of x. And this is now not
a wild pointer. So we had to initialize the pointer to make it
not a wild pointer. And this is basically
how it's done. So let's comment this part, and let's talk about
another type of pointers, which is a null pointer. Now to make sure
that we do not have our endpoint or we can initialize
a pointer with analogy, making it a null pointer. The easiest way to do
that is by simply copying this end asic PTR, which is the pointer
initialization. And then you count
right knowledge. That's it. This is called a null pointer. Now we can also
print this pointer. Hands. You can see
now we are playing, printing it, but
it's not printing anything because it's a null
pointer and empty pointer. Null pointer points at
nothing or at memory address that users cannot access. So in this case, this is
called an old pointer. So let's call this wine. Let's call this dangling, and let's call
this null pointer. Now, another type of pointers
is the void pointer. A void pointer can be used to point at a variable
of any datatype. It can be reused to point at
any data type we want it. And it is declared
easily by writing void asterix point
or PTR equals null. This is called a void pointer, since they are very
general in nature, they are also known
as generic pointers. With their flexibility,
void pointers also brings some constraints. Void pointers cannot be dereferenced as
any other pointer. Now, if you try to
dereference a void pointer, you will end up facing an error. If you are looking to perform arithmetic operations
on void pointers, then you need to type
cast these pointers. Now, typecasting is basically another topic and we're not
going to cover it here. But you need to typecast it if you are going to use it
and arithmetic operations, void pointers out
of great Hughes and C library functions
like malloc and calloc, which dramatically allocate memory returned void pointers. There's also the hue source
and in-built sorting function in C that has a function
as its argument, which itself takes void
pointers as its argument. Now, these mostly come
the void pointer, null pointer, wild pointer, and dangling pointer
usually comes and interview questions
and the MCQ questions. So you need to make sure
that you have all of these inflammations
written down in a notebook so that you can
review it when you need it. And you don't have to
memorize all of them, but you need to know exactly
what each of them is doing. Now, let's do a quick revision. A dangling pointer. The first one is a pointer that points to delete it
or the allocated object. The wild pointer, which
is the second one, is any point or that is
used before initialization. Now to make it not wild pointer, we can do that by simply
using the null pointer. And the last one in our list
is called the void pointer, which is a pointer that can be used to point at a
variable of any datatype. It can also be used to point at any data type we want to use. It is declared by simply
typing void before the pointer name and the asterix sign and
giving it a value of null. That's it for this lesson.
Thanks for watching.