Transcripts
1. Intro of pointers: Hello everyone. In this
course we have discussed about pointers in C. So first, we start with
understanding pointers in C and then followed
by zero point. There are the medical
operations and later related to pointers and functions will be discussed. And then combination of
strings and pointers. Then it will be point does
an ad is combination. Later, we'll be discussing about double pointers and examples related to double
pointers and finally, dynamic memory management
using pointers. So this is the end. Our curriculum
related to pointers. And pointers is the most
important topic in C language. Okay? So make use of this pointers. And this will
definitely help you to perform well in data
structures course. If you're trying
to implement in C.
2. Understanding Pointers: Now we'll discuss about
the concept of pointers. So first of all, what
is meant by a pointer? So generally, goin determines a variable which can store the address of
another variable. So point, there is a variable which can store address
of another variable. So normal variables cannot hold address of
another variable. So that's why we
require concept of pointers in order to store the address of
another variable. Now, I'll show you
an example for this pointer variable
and its representation. Now, consider variable
n is equal to ten. Okay? So this is a
normal variable. Now let me consider
a pointer variable in the star d is representing
pointer variable. Now I'm storing the address of n into this pointer variable. Okay? So if you focus
on this example here, and if I start printing n value, the same n value can be
obtained in multiple ways. So e.g. if I write
person days DNH, I will be able to
display and value. Similarly, I can also use pointer variable
star p in order to display value of n so that it is person-based D star p can be used in order to
print the value of n. So if you consider this here, n is a variable rate debt. So n is a variable and its
corresponding value is. Then star p is equal to address. Often. We are considering
Ibis often as a 2000s. And then p is a
pointer variable, which is holding address
of n and it is 2000. So p is a pointer
holding either soften. Now, we are printing
the end here. So which means this will
display output as a ten. And we're using star P to print, which means how this is
going to be evaluated. Star p represents start
off bees containing 2000. So start off 2000s, which means the content inside
the 2000s memory location. So this star indicates
indirection operator, which means it is updating the contents inside
this memory location. So content inside of this
memory location 2000 is ten. So you're going to ping
result as ten here. Now, we can write two
different pointers. So while declaring,
let us suppose I'm considering different point
does here in the star here. So this is a pointer. So I can consider
this as a pointer, which can point and
in this location. So this is pointer to an
end-user memory location. So similarly, I'm
considering a float star b. So this is considered
as a pointer to a floating point location. So this is pointer to a float. Then car star c. So this is a pointer to
character datatype. Okay? So pointer to a memory location. So like this, you can write
two different pointers, okay? And you should use in teaser pointer only two point
in deserve memory location. Similarly floating
point pointer to point the floating point location,
and so on like this. Now, let us write a
program in order to basically identify the address and our display the content
inside the pointer. Okay, so first, I will
include the header file here. So this is stdio.h. And inside the main function, I'm considering a variable. Inside the main function. We are considering a
variable like number. So let me consider number is
equal to some value like 50. Now we can consider a pointer
variable into star p. So this is a pointer variable. Now P is equal to
address of number, a pointer holding address
of another variable. Okay? So now we can print the address of this
variable p. Okay? So let me print this address
of the variable ISA. So you can use an unsigned
int representation. No, okay, percentage. You print this address. And also you can use your
value representation. So that is value of b. So our value pointed by p, Okay? So this is value pointed to by pointer p is
using person-days D, You can print, which is a star p. So that will print the value that is
pointed by point B. Okay? So like this, you can display address and
also value using point a b. Okay? Now, let's see what are the advantages
of using this pointer. Okay? First of all, you can use this pointer, okay? With the other concepts also. Advantages off point. First one is using pointer. You can reduce code, okay? So this will reduce the
code that we're writing. And also it will improve
the performance. It will improve performance. And you will see this
improvement in performance, especially in C language,
while retrieving. Strings are retrieving or
accessing trees. Okay? Also one more thing,
this pointers are used along with other concepts like structures and functions. So all these topics, so we'll be using
this pointers, okay? So it means whenever you want to access the
elements of an array, one way is to access
through its index. Other ways to access
with the help of a pointer which is pointing to the base
address of the array. Okay? So now another advantage is generally return statement in C can return only
a single value. Whenever you want to return multiple values from a function, you can use a pointer. You can basically return a pointer and using
that point there, you can access subsequent
memory locations. So I can write here
whenever you want to return multiple values or multiple
values from a function. Okay? So you're gonna
use concept of pointers. And also whenever you
use this pointer, so it will allow you to access any memory location in
the entire computer. Okay? So this will
allow us to access any memory location
using pointer. Okay? So now let us write as I'm Bill program
using pointers. Okay? So first, I'll perform swapping of two numbers without
using third variable, but I will use concept
of pointers here. Okay? So first, include
the header file here. Inside the main function. First we can consider
variables is equal to ten, b is equal to 20. Now, we will consider pointer
variables here, star b1, which can hold address of j, n star v2 to find the
address of B. Okay? So now you can actually display the content
before this wrapping. Before performing
swapping, you can display content
pointer by P1 and P2. So this can be written
as before Swap. Okay? Star p1 equal to
using percentage d. Okay? And also I'm considering a star, v2 is equal to
percentage slashing. So here I'm writing star
b1 content and star. We do condemn them. Okay?
Now let us perform swapping, using, okay, without
using any third variable directly you can perform with
the help of these pointers. So star p1 is equal
to star, star p2. Okay? And then here we can
write star p2 is equal to star p1 minus p2. And also star p1 is equal
to star p1 minus p2. And finally, after swapping, you can display the
content like this, print f, the swapping. So now you can display
this star b1 content. Also, star p to content
that you can display here. So this is going to
be star t1, star p2. Okay? So after this
sequence of actions, clearly the content of UEN, content of b will be swapped it. So let me show you how this
is going to work here. So first, you can show here and it is holding
value of initially. Then bees and other variable, which is holding value
of 20 initially. Now P1 is a pointer which
is holding either Sophie. Let us suppose
that the Sophia is 2000s and address of b is 3,000. Okay? So if this is the case, then P1 will be a pointer
holding address Sophia, which means 2000
memory location. So P1 will be pointing
to this memory location. Now, P2 is another pointer
calling address of B, which is 3,000 memory location. And P2 is pointing to this one. Now star p1 is equal
to star p, star p two, which means star t1 is pointing basically
this location right? Now, this gets updated by adding star P1
contained with a star. We do content. Star P1 content is ten and
star P two content is 20. So ten plus 20 will be 30. And that will be stored
inside this location, which is pointed by P1. Then star p2 is equal to
star p one minus p star p2. So star P1 is contained
inside this location, which is 30 minus a star P2, which is 2030 -20
value will be ten, and that will be stored
inside this memory location, which is pointed by P2. And finally, star p1 is equal to star p one
minus p star p, star P1 is 30, and star p2 is 1030
minus ten will be 20. And that will be stored
inside this memory location, which is pointed by p one. So this should be
modified it to 20. Finally, we are displaying
after swapping up, so star P1 content and then
start with to continue. So after swapping star
p when content is our 20 and star p2
content is ten. So, but before swapping, you can see star
P1 content will be pointing to EA and start P2, which means P2 pointer will
be pointing to b, okay? So that is 10.20 initially. But after performing swapping, it is 20.10. Okay? Like this.
3. Pointer Arithmetic: Now we will work on now how
pointer increments operation, endpoint or decrement
operation is going to work. Okay? So first, consider
a variable here, then its value is 50. And I'm considering
another point of variable star p here. So P is equal to address of x. Okay? Now you can bring into here the corresponding
address for x, which is nothing but P. So print f address off be our address inside IP, address, which is
stored inside B. Okay? So you can display it. But I think like
this, r of x Caesar. So you can use a
percentage you in order to display this
address and signed, okay. So B, you can display. Now, let us run this and
see the result of asphalt. You can see this is the
current address of x is. So this is the present. I press, Okay. Now I want to increment this
p by one location, okay? So P equal to P plus one represents incrementing by
one memory location. Okay? So address is equal to address plus increment
by one memory location. That memory location is
equal to size of InDesign. Okay? So P gets incremented y size off in teaser. Okay? Or I can say wanting to sizeof in desert is incremented by one in two
sides of in deserve somebody. If I'm taking p plus two, then it represents doing
those eyes of InDesign, it will get incremented. So finally, when you
want to display here, after incrementation
operation, okay. Increment i address is okay. So using person pays you, you can display
the address here. Okay? And you can start displaying it. Now, let's run this code
and see the reserved. Okay. So if you want, we're going to spin
a single line. You're going to observe this
is the address of x, right? Okay. And after performing
one increment operation, I press is exactly incrementally
by four because the four is representing size of one
in diesel memory location. So that is why it is
incremented by four. Now, if you consider
incremented by four, e.g. then this represents B has to be incremented by four in
these memory locations, which is nothing but four
into size of int teaser, he gets incremented by
four into size of int. So after incrementation
addresses, you can print here. So let's run this code
and see the reserve. So initially this
last two digits, you can see the money dollars
saved last two digits, 80. And here the last
two digits is 96 because the difference
is 16. Okay? It clearly indicates we are incrementing current address
by four memory locations. And each memory location is
equal to this size of int. Size of integer is four bytes. Four into four is equal to 16. That is why it gets
incremented by 16. Okay? Now similarly we will consider for a decrement
operation, okay? So if you want to consider for
decrement operation, okay. So let us modify here
to decrement operation. And also here I'll perform
p equal to p minus one. So here, P will be decremented by one
into size of int. P is equal to p minus
one represents P will be decremented by one into
sizeof in deserve. After the incrementation, the
address will be like this. Okay? So let's run this
code and see the reserve. You can observe the address
of x is last two characters. You can see Haiti now
decremented by one position, which means one memory location equal to the size of integer, which is four bytes. So this should be
decremented by four. So value becomes 76
like this. Okay? Then, now e.g. if you want to decrement
by four memory locations, then p is equal to P minus four, which means P will be decremented by four
input size of int. Size of inducer is
going to be four bytes. Four into four is equal to 16. So p should be
decremented by 16. Okay? So let's see here. So this is 80 and this is 64, last two digits, okay? Reminding all are same. So it is clearly getting
decremented by 16. Okay? So this is how pointer increment and decrement operations will take place.
4. Pointers as parameters to function: Now we are considering a call
by reference implementation or passing the address of the variable as a
parameter to the function. And you can use to hold the
address of the variable. Okay? So let us consider to perform mock sum of
two given numbers. So let us consider a comma
b are the two variables. So now we can read this variable
corresponding values and pass them as parameter
to the function. Okay? So now I'll prompt the user here to
enter any two values. And using scanner function, we can read those values
like person-days, the person takes the
address of a comma b. Okay? Now, we need to pass
these variables as parameter to a function
so that it will compute sum of
these two numbers. So I'll consider find summons
the name of the function, passing two variables
and preserve a coma address of
the S parameters. This function, I'm
using return type as wide so that I can display the result to
within this function. Using pointer. We are performing
here star PTR one comma into star PTR too. So within this function, we can compute a result by adding the content which
is pointed by PT R1 and R2. Because PTR one will
be holding address of a and P DR2 will be
holding the address of B. Okay? Now consider another
variable called aureus, which is a result variable. Now aureus is equal to star
p d R1 plus star PTR two. And finally, you can
print the result. So result of adding Easter Sunday is the slashing and you can display RES value. Okay? So let's run this code
and silver reserves enter any two values, 12.23. And you're going to
observe the dessert after adding is 35 plus 23s. Define. So this is how you can pass
the address of the variable as parameter to the function and compute and
display the result. So the under computation
is done with the help of this pointer
variables which are currently holding the
address of the variables which are passive in
the main function. Okay? So this is also as an example of call
by reference implementation. And this is also considered as using pointers as
parameter to a function. Okay?
5. Pointer to a function: Now let us consider our concept
of pointer to a function. Okay? So you can also use a pointer in order to
point IF function. So just like how we have used a pointer to pointer variable, similar to that,
you can also use a pointer to point
IF function in C. Okay? So and also declaring
the pointer variable is similar to
function declaration. So let me show you
with an example here. I want to basically implement
main function right now. Okay? So inside this main function, I will consider a variable
called Reserved variable. And this point that I
want to point star PTR, I wanted to point to
another function. So let us suppose the function declaration is addition initially is the
name of the function, and this is the function
prototype declaration. Okay, so now how do you declare a pointer which is
pointing to a function? So it should be
declared just like how we write the function. Okay? So star PTR, okay? Like this. Now you can use this PDR 2-point I press of another
function which is VTR is equal to I dress or function name
is addition. Okay? Now, we need to
extract the reserve. Reserve is equal to, okay? So you're basically
calling the function. Generally you will
call the function and its corresponding
results should be stored inside a variable result. But right now, you're
going to absorb a PDR is the pointer which is pointing to the function
called addition. So writing this is nothing but the calling a
function called edition. Okay? So this is equivalent to calling an addition
function here. So now, once you get the desert, you can display that preserved. Okay? So this sum is using
person-based should be. You can display the result. Now let us implement
this addition function. Okay? So here, as I have already declared the function
prototype or declaration I can
implement here. Now, inside this function, I'll basically read
the two variables and the corresponding values. I'm going to perform some. Okay? So those two variables
are in B, okay? And I'll clearly prompt the
user to enter any two values. And using scan f statement, you can read this. Person days be person-days. I press Off a comma b. And finally you can
return a plus b reserved. Finally, you can return
a plus b deserved it. So let's execute and see the reserve to enter any
two values I'm entering. First value is in, an x value is 20, and you can see the
result, the sum is 30. So how this is
obtained is clearly, instead of calling this
addition function directly, we are using a pointer to hold the address of
the addition function. Okay? Using pointer,
we are calling this function and extracting the reserve and storing it inside a variable
called result. And then we're displaying that. Okay? So this is the concept of
pointer to a function in C. And it will produce
character reserves. Just like this, you can
enter any two values, like to six and it will
produce the result as eight. Okay?
6. Accessing string using pointers: Now in this example, we'll see how to access
a string using pointer. Okay? So consider the main function. Inside this main function, you are considering a
character which is a string. So this is STR off
is equal to hello. Hello is this string. Now also consider star PDR. Pdr is the pointer pointing to the base
address of the string, which means always remember string name is representing
the address itself, our base address of the string. So you need to use like
this p d or equal to USTR, which means directly, USTR is indicating
the address itself, so PDR will be
holding that address. Now using PDR, you can display the content which
is pointed by this VTR. Okay? So using while loop, you can display this content. While star PTR not equal
to null character. Star PTR not equal
to null character. Then in that case, you can bring star PTR content. So percentage d star PTR. And you can use PDR to
increment to the next position. So this is going to
be PTR plus plus increments by one
position and move to the next location
and start printing that contender and move to
the next location like this. So let's run this code
and see the reserve. Okay, here, you should
use person-days. See, otherwise. It will display its
corresponding ascii value. Okay? So person-days see to
get the character exactly. So same string,
hello is displayed. Let us suppose you have a
string like hello space world. You will be able to access hello space world
using this code. You can see the reserve Hello
Alice displayed. This way. You can access the contents of the string with the help
of a pointer variable.
7. String Comparision using Pointers: It's now we will implement string comparison using
a concept of pointers. So let us consider a STR r1 contains hello and
STR contains void. Now I want to
perform a comparison between these two strings
with them pointers. So let me consider an integer variable which can hold the comparative reserve. And I'll call the function, which is my STR CMP function, bypassing STR one and STR QS
parameters to this function. And finally, I will display
here, reserve that string. Comparison. Reserve Is person, is B. So we're going to
display result here. Okay? Now, let us implement
this function which is my STR CMP function. So using pointers,
let me consider. One is the pointer
to point to string 1.2 as the pointer
to point string too. Okay? So how will you get the
string comparison Brazil? It is simple where you have to basically compare
two strings and identify the ascii
value difference between the first
non-matching character. Okay? So for this purpose, I compare the two
strings as long as they are equal with
the help of while loop. Okay? First, let us suppose a star is, S1 is equal to star is to start, S1 is equal to star is two. And also you need to check here. Star is S1 should be
not equal to null. Okay? So this is not equal to null. Or I can say null character. Anything is fine. So either this is not null. Our star is two is not
equal to null. Okay? So the first condition is the character located in sites STR one is equal to character
located in STR two, which is pointer by
S1 and S2 pointer. And also consider s1, whatever s1 point that is
pointing is not equal to null. Or S2 pointing is
not equal to null. In that case only we
will proceed further. Okay? So in that case we will increment the S1 to the next position and also
as two to the next position. So S1 will be incremented
and S2 will be incremented. Now, here, I will return the difference
between S1 and S2. So here, return star is
S1 minus a star is two. So that will give you
the difference between star s1 and start as to which is nothing but
non-matching characters, corresponding difference. Okay? So either s1 is not
null or S2 is not null, then only this will,
this particular loop is run, running, okay? So overall, you can see when
this condition is true, then only it will work, right? So this must be true and
also this must be true. So which means the characters should be matching
at the same time. Any one of this string
is not reaching null. Okay? So point, you can see S1, pointer and pointer incrementing
to the next positions. And whenever you
find that there is a non-matching character or
both are reaching to null. In such cases, you have
to immediately find the difference between
these characters, okay? So those non-matching
characters. Let's run this code
and see reserved. You can see string
comparison result is -15 because we are comparing
hello with world. First two non-matching character is directly hit U and W itself. Okay? So h minus w, The difference is clearly
15 characters difference. And this H comes first
when compared to w. That's why h minus
w will lead to -15. So the result is shown as -15. Okay? So let us suppose
I want to modify this string like x, y zed. And also this says x, x, z. Now let's run this code and see the reserved y minus x
is going to produce one. Okay? Like this. Suppose if I'm
giving this as x, x, z, and this is x, y, z. In such case, you will get
a result as minus one. Suppose if the modal
strings are equal, basically x, y, z and x y, z. In such case you will
get result as zero. Okay?
8. String Concatenation using pointers: Now, in this video, I'll show you how to perform string concatenation operation
with the help of pointers. So I lose concept of pointers to perform this string
concatenation operation. Also, I'll perform this
string concatenation in a separate function. Okay? So let me write here string concatenation
using pointers. So first I will include
the header file. Yes, TDI would catch. Now inside the main function. Consider a couple
of strings here. So consider STR one equal to hello and STR equal to void. So consider a bigger size for STR one because I want to
concatenate inside STR one. So that's right. We're going to consider
STR one is our 20 e.g. and then I'm concatenating
the content of SGR, STR one. So let me call it function here, which is my STR CAP function. And I'll pass STR one karma STR to as parameters
to this function. And later, we can
display STR one content. Okay? So string one, nice
person days, yes. You can display as DR1 contents. Now let us implement this
My STR CAD function. So let us consider this
function implementation. Where I consider a
couple of pointers Less. Let us suppose I am
considering star is two and star is S1 here. Inside my STR CAD. Now I want to move to the end of the string that
you can observe for STR one, I am using a stew
as the pointer. Okay? So first you have to use this S22 I trade to the
end of this string. Using while loop you can hydrate this star S2 is not
equal to null character. So in that case you simply
increment this is two by one. And now you need to check
whether this one is okay. So first, Yes, When he's already pointing those
string two right there. So you have to access
character-by-character and append it to this string, STR one string which
is pointed by S2. So this can be done like
this using while loop. I'm going to check like this. Star is S1 is not equal
to null character. If this condition is true, then we can copy
and also increment. In a single statement. I'm coping like this. S star is two plus plus is
equal to star s1 plus, plus. Okay? And finally, after entire
coping is completed, then you just need to explicitly
place a null character. So which can be star is two
is equal to null character. Okay? Like this. Now, let us run this code
and see the reserved. And we're going to see the
updated string is hello world. String one is hello word. So initially string one
contains only Hello, string two contains world. Now after performing string
concatenation operation, which is implemented
using pointers with string1 is pointed by
character pointer S2, and string two is pointer
by character pointer S1. For us, we have moved to the end of string one with the
help of stupendous. And later we started
coping with helper for s1 point
character-by-character into the location whereas
two is pointing. So finally, we are assigning a status to equal
to null character, which means we are indicating the null character at the end of the string
after concatenation. So now we are able to access
the content of STR one, which contains concatenated
result of STI R1 and R2. So this is how it will work.
9. Finding Length of the string using pointers: Now let us compute length of this string using
pointer variable. So you can observe I have taken a string name as
HelloWorld and star. Ptr is the pointer variable is pointing to the base
address, which is STR. Now, you can start counting the number of characters
inside this string, which is the length
of this string. You can consider n equal to zero representing the
length of the string. Now, using while loop, you can either treat here while star PTR not
equal to null character. So if this is the case, then you can increment the
length of the string, okay? Because you have
to count each and every character
inside the string. So alien plus, plus, and also Moody's PDR
to the next position. After counting the
first character, it has to go to the
second character. Okay? So once all the
characters are counted, you can display the length
of the string here. So length of string, ease. Person days should be alien. Using alien a variable, you can display the
length of the string. Now, let's run this code and see the length of the string is 11. So clearly there are a total 11 characters
in this string, including spacer, total
lemon characters are there. This is how you can find
the length of the string. Okay? You can also try passing this STRS parameter to the function and identify the length of the string
and another function. So let me show you here. So PTR is equal to STR. And we're going to implement a separate function to find
the length of the string. So I would write
my own function, which is the MISTRA alien. And here I'll use
the pointer variable like star, PTR pointer. And you're going to see
if PDR is equal to STR. Here. Since we're passing
character pointer, this must be character
pointer as well. Okay? So the idea is to just consider a variable
called EN, okay? To find the length
of the string. And this equal to, call this function My
STR alien function. And pass this STR as parameter. Okay? Because everything
we are computing in the other function, you don't need to take this pointer at all
in this function. So again, this equal to my
STR allele and off STR, I'm passing a STRS parameter
to my STR Len function. And finally, you can display the length
of this string here. Print f string length. String length is here. You can display length of the
string using variable EN. And you have to implement
this MISTRA LEN function. So with the help of pointers, that is character pointer. So let us consider another character
pointer like car star m. Okay? And initially I'll consider
temp is equal to PTR mode, this PTR and temper pointing to the base
address of the string STR. Now, I would like to
only the temple point. So while this star m is not
equal to null character, we can treat that here. So you can move this temp
pointer to the next position. Okay? So since only one line, you can skip this M
plus, plus. Okay? Once the star temp is
pointing to null character, then finally, you
can return here. **** minus PTR. As we know, character location is going
to occupy only a one byte. Okay? So temp is now pointing to the last location in the string, and PDR is already pointing to the first
location in the string. So it will give you
the difference, right? And that will become the
length of the string. Okay? So let's run this code
and see the result. String length is less than. So. This is a string, right? Hello space, world, total
11 characters are there. And we are able to
get the same result. Okay? So this is how you can pass a string name as
parameter to the function and use character
pointer to point to that string and compute the
length of the string here. Okay.
10. Pointers & 1D arrays: Now we'll discuss about pointers and
one-dimensional add is. If you observe this concept of pointers and
one-dimensional arrays. First I'll show you how
this pointer can be used to access the content inside
this one-dimensional array. Okay? So whenever write it clearer. Now, let us suppose
in the age of five. Okay? So you're going to
observe there will be phi contiguous memory locations. So you can see this
is an RNA called yay. Okay. This is integrating the
very first location, the knee off one. This is your 234 like this. Now using pointer, you can access content inside
each of these locations. Okay? Let us suppose I want
to store at compile time the list of values
inside this array. Suppose if I store like
this, 102-030-4050. So in this case, these are the values that are stored and will be stored here. 20 is stored here, 30 is stored here, 40 is stored here, and 50 will be stored here. Now, consider the base address. Let us suppose this is the base address of the
array, which is 2000s. And this is 2004, as in desert is
occupying four bytes. This is 2008, this is 2012, and this is 2016. These are the
corresponding addresses. Now, using pointer, we can actually point to the base
address of the entire array. So this can be indicated, let us suppose using
a pointer variable. So consider in the star PTR. So PTR is the pointer
variable I'm using. This PDR can point to the
base address of the array, address of a of zero, e.g. you can store like this
address of a of zero. Or simply EH, selfie is
representing the base address. Okay? So now when you
perform this action, PDR equal to address
off here of zero, it is going to consider this pointer variable,
corridor, PTR. And it will store the address of the main centers of
the entire array. Are you off zero
corresponding address, which is a 2000s. This will store 2000. Npr will be pointing to
this memory location. Okay? Now using PTR, you can access each and every element
inside the array. So for this purpose, I will take another
loop variable called i. So I'll write a sample
for loop to create an access each and
every element inside this array with the
help of a pointer. So I is equal to zero. And then consider I less than five because there are five elements
inside the array. Now, using print f statement, we can display each
of these elements. Print f, percentage d slash t. Now I display PTR is
pointing to this location. How can I get the element? So when I mean PTR, I get 2000s. When I write a star PTR, I'll get content inside 2000 memory location,
which is ten. And after that I want to
access this content called 20, then 30, then 40, then 50, like this. So in such case I value, we are going to use that
updated I value right? Star off PTR less. I start off with DR. place. So this I value
will be I treated starting from I equal to zero
up to I is equal to four. And this is a star
of PTR place I. So when I start off PDR plus I, this is actually
equivalent to, okay. So you're going to observe
star off VTR less than zero. This is equivalent to
writing u of zero. Okay? When you consider
I is equal to one, this will become star
of PTR plus one. And this will be
equivalent to one. Okay? And writing star of PTR L2, where I is equal to two, in this case it
means equal energy to lay off two and so on. Similarly, you can see here
star of PTR plus four. This is nothing but four, okay? Like this. And if there are n number of elements
stored inside the array. So in that case, you can simply write
star off VTR less. En is equal to e off. Okay? Or it can be K simply PDR plus k is equal to
a of k like that. Okay? So now let me basically read the entire content of
the array with the help of pointers itself and display the content of the
one-dimensional array. So first, take the header file. And inside the main function, I'm going to consider
an array here. Okay? So consider a raise
e off 20 e.g. and I'm using two variables
in non dye inside and you can store the number of elements or length of the array. For this purpose, you can
bring to this statement. Number of elements are
simply enter size. So using scan f statement, you can read that
particular size into variable quality
and address often. Now, we're going to prompt
the user with Scully to lead our enter those
many elements. Here you can ask ender
percent is the elements, which means n elements. We're asking the user to enter. Now using for loop, I'm going to axis, okay? So directly here,
I'm not even taking any point using airy
only I'm accessing here. I equal to zero, I less than n I plus plus. Now we can store using
scan f statement. So this is scan f person
days of the year plus i. Okay? So we're storing
inside the array. Now when you want to display
similarly using print f, You can just say displaying
elements of array. And after that, you can
simply use this for loop. I equal to zero I less
than n I plus plus. And using print f statement, you can display like
this person is D star of a plus i content inside that particular
address is obtained. You can close this
main function. So this code is basically for reading with the
concept of pointers. And this is using the
concept of pointers. We are displaying the content
of one-dimensional array. Or you can directly take
another pointer and point to this base address of the array and start titrating. Okay? So either way you can do it. So let me show you with
an example where I will modify and show
you different ways. So that is considered accessing
the content of the array. Here. In the main function. I'm going to add it here. Let me see off 20
comma n comma i. Now I'll prompt the user
to basically into n value. So once you enter in values
using scanner statement, you can read it
into variable in. Okay. So later, you can
prompt the user to person D is the values. And exactly you need to
get even number of values. Okay? Then using for loop, you can start storing
these values inside this array with the notation
of pointer notation. So using scan f statement, you can write like
this person days of d. Simply a plus I. This denotes here a
plus I is nothing but address of address
off your file. So you can see a plus cy
is equivalent to phi. Okay? Just like this. Now, here. Similarly, I want to
display this content. So using printer, you can
just display array elements. And again, using for loop here. And instead of scanner, you have to use printf statement
to display the content. Deaf person days, the
star of a plus I. This will give you the content inside that memory location. And if I write like this, this represents EOF means it is equivalent to
star of a plus I. Start off a plus right, is equivalent to a or PHI. So let's run this code
and see the result. Okay? So here we have to
use a semicolon. Enter n value, I'm
entering in values five. Now it will ask you
to enter five values. Let us suppose 1020304050
are the values I have given. You can see the same values
are getting printed, okay? So I'll use here a person days, four days so that there will be some space while
displaying the elements. So this time I have
entered a 6,000 value. So consider different
values like 12, 304-203-5663 for P2. Okay, these are the six
elements I have given. Now you can see
displaying our elements 1234, 2035, 0660, 3402. Same. So instead of writing
star of a plus i, okay, you can directly
write e of I. This is a general
notation where we have used while working
with that is okay, So address off, you're off. Let me show you here in a race, this is the notation
we have used. Suppose n values for 122,345.56, see the same value,
so the spread. Okay? Now, instead of using
the acronym directly, you can also use pointers. A great point there,
an integer pointer. Okay? You can use this PTR to point. I dress off the off zero. Now using pointer, you can
access everything here. Simply write PTR plus I. And here instead of
writing your file, you can write start
off PDR. Bless. Okay. So this will also work. This time I have given
n value is five. Elements are n 2030, 40.50. Again, see displaying
our elements 102-030-4050, like this. Okay?
11. Pointers & 2D arrays: In this video, we will
discuss about how to access a two-dimensional array
using concept of pointers. So this is pointers
and 2D arrays. That is two-dimensional arrays. Okay? Now, if you consider
here, generally, NRA. Let us suppose I'm
considering an array which is a two-dimensional
array, like 23. This denotes mescaline Eric, or which contains how many Rosa? So there are two rows
and three columns. That are two rows
and three columns. So row number, and then these are all
column numbers, right? Okay? So if you look at
these locations, this is representing your
office zero-zero, okay? And this is
representing your 01. This is the all zero too. This is representing a 0 of 10, and this is your 11. This is the old 12, like this. Now, when I want to store a value in this
particular location, we generally write address, so you-all for zero-zero. Okay? This is clearly
equivalent to writing start off year plus zero plus
zero using pointers, okay? Suppose if you want to
display this contender, that is the off with zeros, zero content you want to
display using printf statement. Then its equivalent
is in pointers. It is a star off, start off EA plus
zero plus zero. Okay? So these two are equivalent. Similarly, these
two are equivalent. Now, let me show
you here clearly. When I mean the representation as suppose is the Adi, okay? In the, off to three,
the same attic. Or does this year
represents okay? So then I mean here, it actually represents a
pointer to the first row. Okay? So this is
representing pointer to first row directly. So when I write k plus i, it represents a pointer
to the IEP through, okay? Pointer to I know e.g. if I write star off here plus I. And this is
representing pointer to the first element in the
eye and through, okay? Very first element in I, throw somebody if I write, start off here, plus I plus j. So this represents a pointer to the I delivered in the eye. Okay? Pointer to the element. Okay? So I know when I mean, start off, start off
it plus I plus j. This represents the
value that is stored inside IJ location, okay? So which is nothing but
the value stored at, okay? In added representation,
you can see value stored at a
of I j location. This is the meaning here. Okay? So now whenever we
want to basically read the elements into a
two-dimensional array, okay? And also display the elements from the two-dimensional array. So we can write like this. First, I will include the
respective header file stdio.h. Now using this main
function, okay. So far, so you can
see there are and add eight quarterly x and its
size is five-by-five. Okay? And also I consider p, q for indicating
the row size and column Sicer INJ or the
loop variables here. So now first we will print or display the user to enter the order of matrix and
the order of matrix. So you need to read this
order of the matrix. Person, basically person-days, the address of p
and address of q, because p is representing
the row size and q is representing
the column size here. Then you can prompt
the user to enter order of matrix that the
corresponding elements, number of elements is
equal to pin to Q, right? So I can write interpersonal
elements here. And the number of
elements should be between two Q
number of elements. Okay? So once users started entering
those many aluminums, you can read them
inside the array. Using a for loop. I equal to zero to I iterate
through all the row. So i less than p I plus, plus. And similarly, for
j equal to zero, j less than Q, j plus plus. Now, using this
scan of statement, we can read by writing
like this percentage d. So generally nowadays you
will write address off you off or whatever name is here
I have considered are in MS. Excel, so you have to
write our dress off. Off. I j. We have already discussed here. I dress off EX saw phi j is nothing but you have
to write like this, okay? That is a star of
x plus I plus j. So using pointers, it's
equivalent is start off x plus I plus G. Like this. Now this is using a scan f
statement we are reading. And later we're going to
display the content here. So in the form of a matrix, which is a
two-dimensional array, that is matrix content that
you can display like this. So while displaying, again, you can use this
for loop I equal to zero I less than p I plus, plus. This is indicating the row. And then column-wise,
for j equal to zero, j less than Q j plus plus. Now we can print like this, print f person there should be. This is printing purpose. You have to write
star of star off we explicitly plus j, okay? Because this is equivalent
to writing simply X of IJ. Okay? So for two-dimensions,
you require two stars to access the
content inside that location. Then you have to use
new line so that after every row it will move
to the new line to print the next line and
then close this. So this is the
format that we use here in order to basically read and write contents of the two-dimensional array
with the help of pointers. Okay? Now, let us implement this and see if
there are any changes needed. So here for styling the respect you
header file stdio.h. And inside this main function, consider a sample array, which is a
two-dimensional array. Okay? Consider there are two
rows and three columns. And I'll go for compile them initialization for
the first time, okay. 456. And I'll just display
these elements using pointers. Okay? I is a loop variable and
Jason loop variable. Now, using for loop, we can I treat like this, I equal to zero, I less than two, I plus, plus. Okay? Because he is representing
the row size. Now consider column size three. So J equal to zero, j less than three, j plus, plus. You can print this here. Print f. Usually we'll write
person, they should be. Okay. I, j notation. In order to display the content
in the form of a matrix. So line by line. Okay? So if you want, I
can write a message. Tricks. You can display the
metrics content. Now, let's execute
this and see them. So in order to display basically elements with
some space after it, you have to write
person-days WD format, where w is representing
the victim. So I have used up
three characters. One character will be
printed and remaining two characters will
act as spaces. So this is how we generally printer a
two-dimensional Adi. Now I'll replace this with its corresponding
notation using pointers, that is a star off, start off E a plus I plus j. Okay? Now let's run this code
and see the result. You can see matrixes, so there are two rows
and three columns. Okay? The same matrix is displayed
using this lesson. So as I told, there are two indices, because this is a
two-dimensional array. To access the content, you will have to use
a double star here, that is a one star and another star sub l. If you're
writing only a single star, then I'll show you
what happens here. If you're not writing to stars and writing a
single star like this, then you will end up printing
all the addresses, okay? Suppose there are,
since it is art brush, you can see the
result is this one. These are all addresses
that are allocated for this particular matrix. Okay? So if you want to access
the content inside it, you have to use
another star here. Okay? So it is completely close rate. Now let's run and see if you can see like
this you will get. Now, let us suppose if I wanted to basically read the
content of the matrix, then in that case, at runtime, you can
store like this, e.g. this is something
like ten here, okay? And also this is another 1010 by ten
two-dimensional Mary I'm taking. But you can prompt the user to basically enter the
order of the matrix. Okay? So printers order often breaks. Okay? Now you have to read that particular matrix
that is person DSD, person-based, the address
of p comma address of q. Okay? Later you need to prompt the user to enter
those many elements, like endo percent
is the elements. Okay? So which means exactly
into q elements. Once the user starts
entering Boltzmann elements, you can start reading them. I less than p, because p is representing
the row size. And j equal to zero, j less than Q, j plus, plus because j is representing the
column that is a. Okay? So q is representing
this size of the column. And now I use this scanner statement in
order to read these elements. So person-days be okay. As the address I have
already shown you, this is representing
clearly the address, right? So you have to store the element inside of that address and
then you can access it. So again, since we have
taken a p by q as the order, you have to use that p by q order in order to
display these elements. So let's run this code
and see the result. Enter order of the matrix, e.g. order of the matrix
is three by three. It will prompt you to
enter nine elements. The elements are
123-45-6789, okay? And you can now see the matrix
is 123-45-6789, like this. Okay? So this is how you can use
concept of pointers to access and store it in the
two-dimensional array. Okay?
12. Double Pointers: Next one is double pointer, or a pointer to a pointer in C. Okay? So you're gonna, um, so here's a pointer to a
pointer or double pointer. So the general definition
of a pointer to a pointer or a double pointer is a pointer pointing to address off another
pointer, okay. So is considered as point directly at binder
or double pointer. So let me write here
Winder cooling address off another pointer
calling our dress off. And other pointer
is called point. We're pointing to. You scored pointer to a pointer
or double pointer. So what is the general syntax
of such double pointer? Pointer to a pointer? So let me denote
this index sulfate. So the general syntax will
be consider any data type, but it can mean desert or character or float
or double anything. So datatype but star, star, pointer to a pointer. So this variable name
can be anything, but you have to
consider two stars, which represents
a double pointer, pointer to a pointer. Now let's see an example of
how to work with variables. So, okay, pointer to a pointer
inside the main function. Okay? So first, after
including the header file. And inside the main function, I'm considering like this is
available, which is holding. And also I'm considering a single pointer
or normal binder. And this is a double pointer
or a pointer to a pointer. Okay? So first I will
store the address of a to point B and address
of b into pointer pb. Okay? Now, you can display the content using Star KPI
or double star p here. So I'll show you whenever you want to display the value of k, you can write like this. Or you can also make use
of a pointer variable, which is a star p. You have another way of
accessing the same content. Both pointer to a pointer
that is present is d double star p. Okay? So using which you can access content inside
the memory location. Yeah. Okay. So let us see here how
this is going to work. First of all, is a variable and it is holding
the value of ten. Okay? And P is a point. The ruling Agisoft, which means consider
are there Sophia is a 2000 memory location and the fees if pointer
holding address Sophie Sofia is a 2000 and p is pointing
to this memory location. Okay? No, every
pointer will also have an interest rate because that will be stored in
some memory location. And memory location
will have an address. Let us suppose 3,000 is the address of
pointer variable p. Now, this pointer variable
corresponding address will be stored inside
another point, which is a pointer to a pointer. So this is representing
pointer to a pointer and we'll be holding address
of p. So 3,000. You can see BB pointer to a pointer is holding
address of p. Okay? Now when you start
printing value of E, So this will give
you result as a ten. Okay? Then when you start
printing star p. So let's see how this
is going to be evaluated. Star p represents a star. B means 2000s. Content inside Pluto's and memory location
will be displayed, which is ten again. Okay. Now let's see how this pointer to a pointer
will be evaluated. So here we are printing
clearly double star BP, which can be evaluated
as a star. Star. Pb content is 3,000. From here to here, content inside 3,000
memory location. So what is the content inside
3,000 memory location here? So content inside 3,000 memory
location is clearly 2000s. So start off 2000s, which means content
inside memory location, The answer is ten. So to display the same, then you can perform
these actions. So these are the multiple ways to access the same content. Which means the same content
can be modified either directly using the variable
or using a pointer variable, or using a double pointer
or a pointer to a pointer. Like this. You can write a program
which makes use of a pointer to a pointer and
also a single pointer. So now let me show
you an example, okay, for this
pointer to a pointer. So far as to reconsider
the header file. Then inside the main function. So let us consider
a variable like E is equal to ten and star, double star is
pointer to a pointer. Now I use P is equal
to I dress off. And another one is
Pb right there. So Pb is holding the
address of p. Okay? Now in order to
display value of Yi, you can access in multiple ways. Here. I'm using new line, okay? Directly, you can
use variable yet. Okay? So here I'll write
that a is equal to. Now similarly, I want to access with the help
of a single pointer, which is a star p, star
p corresponding content. I'm going to display here. And after this, we need
to print the double star p. Star p is equal to percent is the slashing
double star p. Okay? So now let us run this
code and see the reserve. So here invalid type argument of unitary operator
star have in-depth. So let us declare like this. Okay? So consider a
double star B, okay? And this is single star p. So which means value
stored inside this, right? Okay? And he just showing
in nine line here. Okay? So one thing I do here, this is int star p is equal to, and this is e1 star, star p equal to. Invalidate a type of
argument or funerary star. Okay? Sorry, here.
The problem is, here we have already
taken PSD single-point. They're under double
star P. Double star p, which is the name of
the double pointer. So we have written a single p. That's right. It's showing that it's basically PPE is the name of the double pointer
we have taken. We have to use the same name. You can see that it's
earning a is equal to ten star p is equal to ten. Double star p is equal to ten. Okay? So like this, you can display
the content, okay?
13. Example program in Double pointers: Let me consider an example which involves a single pointer
and double pointer. Okay? So let us consider
X is equal to ten. And I'll consider our different
variables like star p is a single pointer and double star PTR is a
pointer to a pointer. Okay? So here I consider p is equal to
either Sophie Excel. Nper is equal to
address of p. Okay? So which means if you are
performing any action through pointer p or
through double pointer PTR. So that will affect the memory location
which is allocated for variables except the content inside variable x gets updated. So let us suppose
I am performing double star PDR is
equal to two, okay? Single star p plus 25. Okay? Now finally, when you
print the value of x, this will affect the value
of x or zero value of x. Okay? So let me show
you the result. The result should be 35, okay? So why 35 is the answer? Because you can
observe double star PTR is actually representing the content inside variable X. Now this is going to be here, star p plus 25. So p is pointing to a memory location x and
it contains value ten. So ten plus 2510 plus
25 value will be 35. So PTR is the pointer
which is pointing to that memory location and its
value gets updated to 35. And later we are
displaying x value. So right now the x
value is updated to 35, which means this is the
same location where we are referring to x and
also equal to star p. So all these are
all equal to 35, x is equal to 35. Startup is that if I
double star PDR is 35. So this is how you can
manipulate the content in a memory location using a single pointer or double
pointer. Like this.
14. Dynamic Memory & Functions: In this session, we will discuss about dynamic memory management. Okay? So this is also termed as
dynamic memory allocation. So generally, what is the problem with this
static memory allocation? Okay? So if you consider,
in C language, we often use this
static memory location. An example for our static or
fixed memory location is, let us suppose you are
having declared in d of five and then semicolon. So this actually represents, it is going to allocate five
contiguous memory locations. You'll allocate five
contiguous memory locations just like this, 1,234.5. Now, this location is
representing a of zero. So this is your f1. This is the off to three. And this is here for like this, there will be five
contiguous memory locations. Now, the problem with the
static memory location, so this is considered
as a static or fixed to the way
of allocating memory. So the problem here is if you
want to store ten elements, you cannot store
because the memory is allocated only four or
five contiguous elements. If you are using
only two elements, then reminding memory location, that is five minus two equal to three locations will
be clearly wasted. Memory is visceral
for three locations. Okay? So this is a problem with static or fixed memory location. Okay? Now, to overcome this, in C, we have dynamic memory location where you supply
value dynamically. And the memory is allocated based on the value that
you have supplied. Okay? So e.g. you are taking
n value at runtime and it can create the n
contiguous memory locations as per your requirement. Okay? So that is considered as our
dynamic memory allocation. Here. There are different dynamic memory allocation
functions available in C. Okay? So those functions are first
one is malloc function. So this is a malloc function. And using malloc generally we allocate whatever requested
size of whites by the user. Those many bytes of memory
will be allocated and the pointer will be pointing to the first byte of the memory, which we will be discussing
shortly in detail. Nixon, other one is catalog. So this is another dynamic
memory management function where it is going
to allocate space for an area of elements. And it will also initialize all of these
elements to zero. And it will return the pointer
of the first location. Okay? Then coming to free. This is useful to basically deallocate the memory
space which is allocated. Previously. You might have already
allocated some memory space using malloc or calloc,
but using free, you're going to deallocate
the memory that is allocated using
malloc calloc, okay, next coming
to really lock. So according to real Lock, whenever the
dynamically allocated memory space is not sufficient, then you have a
chance to relocate. How much you wonder. Okay? So for that purpose, relocation function
will be useful, that is realloc function. So it will basically
help you to modify the size of whatever
allocated space you have done previously. Okay? So these four are the dynamic memory
management functions. Okay? So let's discuss in detail
about the very first function, which is malloc function. Okay? So as this malloc function is going to allocate
the block of memory, it will allocate that whatever our block
that you specify, how many blocks you want. Okay, so those many blocks
of memory it will allocate. And consider how you
are going to allocate this and how the pointer will be pointing to the
first location. So the general syntax
of the malloc is PTR is equal to
the dot type star. You have to specify like
this datatype star and then malloc off by its size. So this is the general
syntax of using malloc. Okay? Now let us consider
an example here. So here I'm going to consider a pointer variable
called int star p. Now p is equal to into star. Okay? I'm applying malloc here into
star malloc off bite-size. Okay, let us suppose I'm
writing like this phi window, size of five into sizeof int. So this is going to basically allocate five into
size of inducer, how many bytes it will
return the bytes as writer. So size of integer is
basically four bytes. Then five into four
is equal to 20. So this will allocate 20
bytes of memory space. And this will be pointed
by the pointer p. Okay? Point a, b will be pointing
to this very first location. So let me show you how
this is going to be done. Okay? So first of all, it is creating phi contiguous
in memory locations. So phi contiguous in
memory locations. So this is 12345, like this. Okay? And consider the very first one is a location called
thousand e.g. this will be thousand four. This is thousand eight. Okay? This is 1012 and this is 101.6. So like this, phi contiguous memory locations
will be created. And p will be a pointer holding the base address
of this particular Adi. So p is a pointer. And this is pointing to the
base address just like this. Okay? So now you're going to observe five contiguous memory
locations are created. And p is a pointer which is pointing to the base address
of the entire array. Okay? So this is done with
the help of Melaka. Suppose if memory
is not allocated, in that case, p will
be pointing to null. Okay? Suppose there is no memory space of phi contiguous
memory locations. In that case it has
to return null. So generally, okay, if I use a small number,
so it will allocate. But let us suppose you are
given very large number, okay? And that contiguous
memory space, okay? So it doesn't have that
much of memory space. In that case, it is
going to return null. Okay? So this is about malloc. Now, quickly, we will see
about one more function. In order to write any
program may require another function which
is free function. Okay? So I'll first discuss
about free function. Why? Because you have
to deallocate to whatever memory that is
allocated by you, right? So it is user to
de-allocate memory, which is located using
pointer de-allocate memory. If you look at the general
syntax for free here. So you can write it
as free off PDR, writing free off PTR. So it will free the memory
allocated by the pointer PTR. Okay? So now let us consider the same example which we
have considered already. There is an int star p. P is equal to int star malloc
of five into size of int. Now, after this, you want to deallocate whatever
memory that is allocated. So you can do this
with the help of free writing, free of p. Okay, So this will deallocate
the memory which is allocated using
pointer p here, okay? And in order to make use
of this functions, okay, whichever part of dynamic
memory management, you need to include its
corresponding header file, which is malloc dot hij share. Okay? So you're going to include
malloc dotted share. So let us consider
writing a program here. So where you are
going to allocate memory with the help of malloc. Okay? So first include the
standard header file, which is a stdio.h. Now you want to use this
malloc functionality. So for this purpose, include malloc dot hij. This is malloc dotage. Now using main function. Okay? First-time taking
a pointer because you need pointer to point
to the base address of the memory allocated
into star IP, DR. okay. And n is a variable and
another variable I'm using AI for basically
hydration purpose. And n is representing how many elements
you want to store. Okay? So let us allocate memory
based on n value using malloc. So first we can prompt the
user to basically enter n value or ask the user to
enter number of elements. So how many elements are
there? You have to enter. So after entering the
number of elements, then using scanner if statement, we can read that value
into a variable. So that is percentage d address of person-days they
are dissolved in. Okay? So using this
n value, okay? So we can allocate using malloc where I PDR is pointing to the base address of the
memory are located. Here is equal to. Now it has to typecast doing desert pointer because
we are trying to store all integer
values here, okay? And allocate using malloc, how much of bytes
you are allocating. So which is nothing but
the yen into size of int, size off in busy. So this will allocate memory
for E&M desserts, right? Okay. And after this, using n value, you can start storing elements with the help of
scan f statement that okay. So let's see how
this can be done. Is, first of all, I'll prompt the user to
enter these many elements because you have
already taken n value. So you have to play a
number of elements. I'm writing enter person
days is d elements. Then user starts entering
those many limits, okay? And once those
elements are entered, okay, then you have
to start storing it into the memory
allocated using malloc. So this can be
done with the help of a loop variable
i equal to zero, I less than n i plus, plus. Okay? Using scan f statement
you can store, right? So that is a scanner. Percentage should be this
is a DR plus I plus I. So the base address is
pointed by VTR, okay? When I value is zero, force it will store the
element in that location. Then it is incremented by eye. It means it will go
to the next location and store the next element. So this is how scanf works here. Now we want to display all the elements which are
stored here right now. So you can write a print f statement saying
the list of elements. Okay? The list of elements. Okay? Now, using for loop, you can iterate and display all these elements
like I equal to zero, I less than n i plus plus. Now start displaying
these elements with the help of
fprintf statement. Okay? So where do you want content
inside the memory location? So use a star because you're trying to access with the help
of pointer variable IV TR. So star of ICTR plus I, using which you can access the content inside each
of these memory location, which is pointed by IPT. Okay? So once the accessing is done, you just need to
deallocate the memory which is already
allocated using malloc. For this purpose, you have
to use a free off I PTR. We're deallocating memory
which is allocated using IP, that is using malloc. Okay? So this is the entire program. Okay? Let's see how this program is going to work for us to fall. In the output screen. Generally. It will ask you to first enter number of elements because program execution starts
when the main function, and here it is, printing this
statement right there, which is in the
number of elements. So let us suppose
you have entered a number of elements as fiber. Okay? Then what happens here? It will create phi
contiguous memory locations. Okay? So consider here. How this creation happens
is using IP TR, okay? I PDR will be the pointer
which will be pointing to whatever memory that is
allocated using malloc. Okay? Here we are going to
say N is equal to five, which means it will create a phi contiguous
locations like this. Okay? No. Malloc has to
typecast. After that. We are performing
typecasting into desert. By default, it will be a wide pointers are
generic pointer. So that's where you need to type converted into its
corresponding datatype, but as IBD or is an
integer pointer. Okay, you have to type
converted doing desert. Once the memories
are located like this for n value equal to five. Okay? Then if you look at the
next statement here, enter percentage d elements, which means it will prompt in the output screen saying that enter five elements because
n value is five here. Okay? Now you can start entering
those five elements. That is, suppose you
have entered 1020304050. These elements are entered. So then these elements
will be stored one after the other here, 1020304050. So why? Because since I
value is zero, okay? So I PTR plus zero will be
representing this location. Okay? So consider this is
our dress thousand. This will be thousand, 4,008. This is 1012, this
is 1016, okay? Now using IP TRE are pointing
to the base address, okay? Now we're going to display
the list of elements here. Okay? So finally, it is
going to display the list of elimination. The list of elements. What are the elements
that you are displaying is using star. You're trying to access each
and every element here. So the same elements will be displayed when I equal to zero. This element will be displayed in the next iteration
when i equal to one, okay, I PDR will be incremented
by one memory location. So this element of 20 will be displayed and so on like this, you are able to display
all these elements, okay? And finally, free
or five VTR, okay? So which means it is going to basically deallocate the memory which is allocated using ICTR. So you are no longer will be
able to access that content. Okay? So this is how the malloc
works and also free works. Okay? So this is how you can
allocate memory using malloc and deallocate the
memory using free. In dynamic memory management.
15. Malloc() Example: So now in this program I'll
show you how to basically use this malloc in order to
allocate memory dynamically. So first English or
D header file, okay? And also include a malloc
dot h header file. Okay? In the main function, first, consider the pointer
called a star I VTR, okay? And then I is the loop variable. Then n is representing a number of elements
that you want to store. Okay? So I'll just prompt the user to basically enter the elements. So number of elements. Okay? And using
scanner statement, we can read n value
here, either soften. Now using this
print f statement, we can prompt like this, enter percentage d elements. Okay? So that is n number of
elements you have to enter. Okay? So now using a for-loop, you can read those elements
like I equal to zero, I less than n i plus plus. And using statement that, okay. So once you want to basically
enter and store, okay. So what is required is you have to allocate memory, isn't it? So once user starts entering
the number of elements, so you have to allocate memory, which is using malloc
you are allocating here IPR is equal to, okay. So I've gotten wish near. And then using the malloc off, I'm allocating in locations. And each location is
often dissident, right? So this is size of InDesign. Okay? So honestly memories
are located now. We are going to store here. So that is a IP DR plus i. Okay? So this is how
you can store here. And then now we wanted to display this elements
which are stored here. So I can just the
print f statement saying displaying elements. So in order to display elements, you require, again this
for-loop to I trade. Okay? But print f statement
to display the content. So percentage d. Okay. To access the content
inside it using star, you can access
here I PTR plus I. And finally, we will deallocate the memory which is
allocated using IP DR. Okay? So let's run this code
and see the reserved. You can see it is asking you
to enter number of elements. I'm entering value as five. Now. I'm entering 102-030-4050. Okay? So the same elements that
are getting displayed here. If you want them to be
displayed in some format, okay, Then you can use
this person, this 40. So let us enter this time. Only three elements, okay? Those three elements
are like 102030. So we can observe
here displaying those three elements, 102030. Whatever n value
that you are giving. Those many contiguously
allocated memory is created. With the help of dynamic memory management function,
which is malloc. Pointer will be pointing
to the first location. Okay? So was this time I'm entering into number of
elements as a ten. So let us suppose those ten
elements are like this. Okay? Now you can see here displaying
elements 1234 up to ten. All these elements
are displayed here. Okay? Now what happens
when you are trying to deallocate first and then trying to
access the content. Let us overlay I'm replacing
the statement I'm removing here and placing here. Okay? Then what happens is you're not able to
access that content. You can see because the
memories are located. But later you have deallocated
the memory, right? So whatever content, some garbage values
that are stored in those locations will
get printed here. Okay? Because you have allocated
memory using malloc here. And later store the content using pointer you have stored. And later immediately you
have deallocated memory. Now you're trying to
display the content, okay? So in this case, what happens since it is
already be allocated? Okay? Now you're trying to display in those locations there
will be some garbage values, so they will get printed. Okay? So the tray, you
must do this via location of your
uses is completed. Okay? Like this.
16. Calloc(): Now we'll discuss evolve. Next function, which is dynamic memory
management function called catalog. So generally in this
catalog function, it will actually allocate. Just like malloc. It will also allocate memory. But here we are going to allocate mostly these
blocks of memory. Malloc. You can say this kind of bytes of memory that
you're relocating. But in catalog, you're
allocating blocks of memory, like N number of blocks you want delegate,
you can allocate it. So it allocates
blocks of memory. Okay? Now, let us consider
this syntax. So Kellogg, Kellogg
syntax can be PTR is equal to data type star. Okay? So you have to write catalog. So generally in malloc you will writing and mean two
size of data type. But here it is the N
comma size of data type. There will be two arguments
for Kellogg function. One is number of blocks
followed by size of each block. Okay? So here you have to
supply size of datatype. Okay? So this is the
general syntax here. Okay? So now once you allocate
memory using catalog, so the memory containing
different locations, okay? So they will be automatically initialize it to zero, okay? Each of the memory block
will be filled with zero. So let me show you here
how this is going to work. So consider an example. Like in the star p.
So p is the pointer. Now, B is equal to. So using catalog, we're
allocating memory here. So the star Galois, cough. Let us suppose I'm writing
ten comma sizeof in desert. So here it is going
to allocate memory, which is a thin locations
of memory, okay? And each location or block is off size of int desert type. And it will fill
automatically in each block containing
data as zero. Okay? So let us consider here. B is the pointer, okay? Pointing to the base address of the memory,
which is a located. So this is first 1, s one, and so on, like this. Okay? Suppose you are considering
dislocation as 2000s. And size of integer
is four bytes. Okay? So you will see like
this and so on. Now, we will be holding the
base address of this memory. And each location will
be automatically filled with zeros initially,
according to catalog. The primary difference
between Carlo can malloc is in catalog. We're passing two arguments. Okay? Whereas in case of malloc, we're passing a single argument. In case of scallop, we are allocating
blocks of memory. That malloc, we are
allocating bytes of memory. So now this is how the
entire memory is located and the differences in catalog each of these locations will
be initialized to zero. Whereas in case of malloc. There will be garbage
values, okay? There'll be garbage values. So let us write a program which makes use of this
calloc function in order to create
memory dynamically. Okay? So first let us include
the header file, which is stdio.h, okay? And also include malloc
dot h header file. Okay? Now consider the main function. Okay? So inside this main function, I take your point. A variable that is just
like previously we have considered IBD are similarly here also I'm considering a
pointer variable star I PTR. Now, we need to prompt the user to enter
a number of elements. So here, the number of elements. Okay? So once the user enters a number of elements than using scan f, we can read this
number of elements like person-days DI brush often. Okay? Now, using IV TR, we can allocate in
locations, okay? So we call it as n
blocks of memory can be allocated using calloc all in desert blocks I'm using here. So in Star Catalog off in Goma, sizeof, in desert,
in coma sizeof. Okay. Now we need to prompt the user to enter
those many elements. Okay? So that is interpersonal
tastes, the elements. Okay? So once you enter
those many elements, they have to be stored, right? So whatever memory that is
allocated using catalog, using which you can
store these elements. I equal to zero I less
than n I plus plus and using scanner, okay, you can store here, that is a person days
of the IBT are less. Okay? So you can store
the elements here. Now. You can display the
elements just like this. Print f, the list of elements. Okay? So you can write like this, the list of elements. Okay? Now, you can observe
here i equal to zero, I less than, I, less Press. Okay. Now let's print these elements using the pointer we can access. Okay? Like start off IPT r plus i, okay? You can access. Finally, you're
able to basically read the elements into it and
also display the elements. Now we can deallocate the
memory which is a located. So again, write free of IP DR, so that whatever memories are located will be deallocated. Okay? So if you look at this program, okay, compare it to
the malloc program. This is quite similar, except this particular
line is varying, where you have allocated
memory using catalog here. And the syntax for the Kellogg. Kellogg off in commas
size of datatype. Okay? So this is the only change. Okay? So let us work on it and
display the content. Whatever is taken. No, here. Let us read this I-beam pointer, okay? In the end also I. So here we will prompt
the user to enter. Number of elements. Number of elements. Okay? So using scan of statement
though, we can read it. So percentage should be okay. Now using IP TR, we are going to allocate
memory using calloc. So here in Star, Catalog off in karma size often. Okay. So now let us enter
those many elements. So we can ask the user to
enter those many elements, which is n number of elements. Okay? So once you start entering those many elements using
for loop, you can read it. I equal to zero, I less than n I plus plus. Using scanf statement, we can
read it percentage d, Okay? I VTR plus i. Okay. Now we can start displaying
those elements here. Print f, okay? The list of elements
you can display. Okay? So using for loop
for I equal to zero, I less than I plus, plus. Okay? Using print f statement, we can display
printer, person taste. Be. Okay. Suppose I want some
spaces while displaying. You can start off I PTR plus i. Okay. Finally, you can deallocate
whatever memory that is allocated using the
pointer IBD are. Okay with the help of
catalog you have allocated. Okay? So let's run this code
and see the result. Enter number of elements. I have entered number of
elements as five here. Okay? So the elements
are 102-030-4050. Okay? You can see these are displaying
those elements exactly. What happens when I remove
this free of IP DR. here, and use here. Okay? So let's run this
and see the reserve. Five elements and 2030, 40, 50. Okay? Again, you can see here you are de-allocating memory, okay? So after deallocating
the memory you're trying to access rate,
that is a problem. Okay? So in that case you will
get some garbage values. Okay? So that's why you should
always use this free of IBD are here after the
allocating the memory. That is after accessing all the elements and then
start de-allocating memory. One more thing is suppose
you're not going to use these lines like
entering elements, okay? And storing those elements
into this memory. In that case, what
will be the reserve? Okay? So let us suppose I have
entered values five. And you can see the
list of elements are zeros, zeros, zeros 00. Why? Because according to catalog, once the memories
are located, okay, it will replace all zeros in those memory
locations by default. So that's why you are getting
default values as zeros. Okay? So this is how catalog works.
17. Realloc(): So next one is real lock. Okay? So in case of real law, we are going to relocate the memory if the currently allocated
memory is insufficient. So the waters a little
memory allocation can be done using
malloc or calloc. You will start
allocating memory later. You want to expand memory, okay? So you can start using real OK. Okay, using the log, you can reallocate memory. General syntax for this. Real lock will be, you can write like
this new PDR is equal to data type star. Real lock datatype star, yeah, Lock off all the
PDR comma new size or the PDR common size. Okay? So this is real lock
corresponding syntax. So what is meant
by old PTR here? Okay? So here I can write where old PTR is
actually representing the pointer that is
pointing to the block of memory which is
already allocated using malloc or calloc, as they told horizontally, will be allocating some memory. Using malloc or calloc write. This world pointer is
actually representing the pointer pointing
to the old memory. To memory. And this memory is allocated
using malloc or calloc. Okay? So that is representing
this oral memory. Now, what is new sun is? So new size will be representing updated size that you want to
allocate using real locker. So I can write here as size
of the new block, okay? Or size of the block
to be relocated. So again here also, once you relocate
memory, new point, there will be no pointing to
that newly allocated memory using real OK. so let me
show you an example here. So consider an example
using real aka first-time considering
a pointer in disappointed here in the star b. Now, I'm allocating memory using malloc for this
inducer pointer. So this is in the
star malloc off. We can write e.g. I. Want to allocate
n memory locations. So ten into size off into
ten into size of int. Okay? So it will allocate ten locations and each
is of size of integer. Now, we can allocate
using yellow ocher. So if already allocated
memory is not sufficient. Ok? So you're going to
locate using real lock. Let us suppose I
want 20 locations, new sizes, 20, e.g. so in that case I can write
into star real lock off. So what is the world pointer here I'm using the word pointer, a new pointer, both
as same P only. So old pointer, common new size. Right now I'm
allocating new sizes. 2010 locations or ten
blocks are located. Now it is 20 blocks. We are relocating. The world pointer and new
pointer both are same, which means the new
pointer will be the same point p
will be pointing to the newly allocated memory. Okay? So previously here
you can see there are ten blocks of memory. Ten into four is equal to 40 bytes of
memory is allocated. But using this, this is
going to be 2020 into four, which will be 80 bytes of
memory will be allocated. For both of these, we are using the same
pointer to point. P will be now pointing
to the updated memory, which is 80 bytes of memory. Okay? So now let us
consider an example here. For real aka. Okay, so first I will basically consider allocating
for character pointer. Let me implement directly. Okay? So I'm using a
character pointer here. I'm using it character pointer. So where I can write it
as cars star name, e.g. okay, star name. So this is my character pointer using the term
allocating memory. So that is name is
equal to, okay. So star malloc off. Okay. So here let us
suppose I'm allocating ten contiguous locations and each is of character datatype, but so which means size
of caries, one byte, ten into one is ten bytes of memory I'm allocating
using malloc. Okay? So now I want to copy
some content into this. You can make use of
STR CPI for copying. But for that purpose you have to include another high profile which is a string
dot h header file. String dot h header file. Okay? So once I have included, now we're free to use
this STR to see BY. Okay. So I'm writing on placing
a string called Hyderabad. Okay. So after copying this, now we are trying to
print the name which is located at that
particular address. Okay, let us suppose I
will try to print them. The name which I'm storing,
which is a hydro bag, and that is stored
at address. Okay. So the iris is percentage p. Okay? So we can write it as
a name, comma name. So you can absorb a
first-time name will be printed in the exact
string format, okay? But second time you
can see the same name, but percent SBUs user, which means the address,
will be displayed. Okay? Now, this is using malloc
we have allocated, let us suppose I want to
store a bigger string. Okay? So in that case, this is
not sufficient writer, so I'll make use of free lock. Let us suppose star. Okay. So I use realloc and allocate here. Let us suppose some 15
characters and trying to store. So this is a sizeof
character type. Okay? So here also semicolon,
here also semicolon. Okay? Now I'm trying to
copy another string. Okay? Let us suppose name comma. So we just need to take
a bigger string then. Okay? So I'll take a string
like mini off for this. Okay? So the number of characters are greater than number of
characters in Hyderabad. So now let us print
similarly here also. Right now the name, corresponding address also am trying to display
along with the name. Okay? So once your users is completed, you can just deallocates whatever memory that
is allocated here. So free of name, using which you can deallocate memory which is located here. Let us run this code
and see reserved. Okay, so I tried
to use here, okay. So already we're printing
in a different line only. Okay? So right here a slashing
and here also slashing. Now you can see name is
hydro bot, isn't our dress. Okay. You can observe the
corresponding address allocated for this. Now name is minneapolis, and it is normally diapers
this particular address. Okay.