C_74 Pointers in C- part 4 | Pointer to Pointer (Double Pointer)
Summary
TLDRThis video tutorial delves into the concept of pointers in C programming, specifically focusing on double pointers or pointers to pointers. The instructor begins with a review of basic pointers and operations before advancing to the double pointer concept. Through detailed explanations and visual memory representations, viewers learn how to declare, initialize, and use double pointers. The tutorial also covers practical examples and code demonstrations using VS Code, ensuring a clear understanding of how double pointers function and their importance in C programming.
Takeaways
- 📌 Pointers are crucial in C programming for handling memory addresses.
- 💡 The 'address of' operator (&) and 'indirection' operator (*) are fundamental to understanding pointers.
- 🖥️ Pointers can store the address of another variable, enabling efficient memory management.
- 🔄 A pointer to a pointer, or a double pointer, stores the address of another pointer, not a normal variable.
- 📝 Declaring a pointer to a pointer involves using double asterisks (**), indicating two levels of indirection.
- 🔧 Properly initializing pointers is essential to avoid incompatible pointer type errors.
- 🖱️ Accessing the value of a variable through a single pointer uses one asterisk (*), while a double pointer uses two asterisks (**).
- 🔍 Visual representation of memory allocation helps in understanding how pointers work.
- 📊 The concept of pointers can be extended to multiple levels, such as triple pointers, to manage more complex data structures.
- 💻 Practical examples and coding in an editor like VS Code solidify the understanding of pointers in C programming.
Q & A
What is the main topic of this video?
-The main topic of this video is the concept of pointers in C programming, specifically focusing on pointer to pointer (double pointer).
What are the two operators discussed in relation to pointers?
-The two operators discussed are the address-of operator (&) and the indirection operator (*).
What is a double pointer?
-A double pointer, or pointer to pointer, is a special pointer that stores the address of another pointer.
Why is the concept of double pointers important?
-Double pointers are important because they allow for more complex data structures and memory management techniques, such as dynamic memory allocation and manipulation of multidimensional arrays.
How do you declare and initialize a double pointer?
-You declare a double pointer with two asterisks (**) and initialize it by storing the address of a single pointer. For example: `int **q = &p;` where `p` is a pointer.
What is the correct way to access the value of a variable using a double pointer?
-To access the value of a variable using a double pointer, you use the indirection operator twice. For example, `**q` will give you the value of the variable that `q` ultimately points to.
What happens if you try to store an integer value directly into a pointer variable?
-If you try to store an integer value directly into a pointer variable, it will give an error because a pointer variable should store an address, not a direct integer value.
How can you print the address of a pointer variable?
-You can print the address of a pointer variable using the address-of operator (&) and the %p format specifier in printf, e.g., `printf("%p", &p);`.
What error will occur if you assign the address of a normal variable to a double pointer?
-Assigning the address of a normal variable to a double pointer will result in an incompatible pointer type error because a double pointer should point to a single pointer.
How do you change the value of a variable using a double pointer?
-To change the value of a variable using a double pointer, you assign a new value to `**q`, where `q` is the double pointer. For example, `**q = 25;` will change the value of the variable pointed to by `q`.
What is a three-level pointer, and how do you declare it?
-A three-level pointer is a pointer to a double pointer, allowing for even more levels of indirection. You declare it with three asterisks (***). For example: `int ***r = &q;` where `q` is a double pointer.
How can you access a variable using a three-level pointer?
-To access a variable using a three-level pointer, you use the indirection operator three times. For example, `***r` will give you the value of the variable that `r` ultimately points to.
Outlines
🔍 Introduction to Pointer Basics in C
The video begins by introducing the concept of pointers in C programming, explaining the basics of pointers, the address-of operator (&), and the indirection operator (*). It covers how to assign a pointer value to another pointer and emphasizes the importance of understanding pointers when dealing with memory addresses. The explanation includes visual aids to depict how memory allocation works for variables and pointers.
🔄 Understanding Pointer to Pointer (Double Pointer)
The video delves into the concept of a pointer to a pointer, also known as a double pointer. It explains that a double pointer is a pointer that stores the address of another pointer, not a regular variable. The explanation includes a detailed example, showing how to declare and initialize a double pointer. It highlights the importance of the data type in pointer declarations and how the compiler interprets these declarations.
💡 Practical Examples of Double Pointers
The video provides practical examples to illustrate the use of double pointers. It explains how to declare a double pointer and assign the address of a pointer to it. The example demonstrates the memory allocation for a double pointer and how to access the value of a variable using a double pointer. The explanation includes step-by-step instructions and visual representations to make the concept clear.
🔢 Multi-Level Pointers and Their Use Cases
The video extends the discussion to multi-level pointers, showing how to declare and use triple pointers (pointer to a pointer to a pointer). It explains the rules for assigning addresses in multi-level pointers and demonstrates how to access a variable's value using a triple pointer. The explanation includes examples of how to print addresses and values at different pointer levels, emphasizing the correct usage and potential errors.
⚙️ Implementing and Testing Pointer Concepts in Code
The video transitions to a practical coding session, where the concepts discussed are implemented in C using VS Code. It shows how to create a file, write code to declare and initialize pointers, and print variable values using single, double, and triple pointers. The session includes debugging tips, explanation of common errors, and how to resolve them. The video concludes with a summary of the importance of understanding pointers and their practical applications in programming.
Mindmap
Keywords
💡Pointer
💡Address-of Operator (&)
💡Indirection Operator (*)
💡Pointer to Pointer (Double Pointer)
💡Memory Allocation
💡Dereferencing
💡Hexadecimal Address
💡Initialization
💡Dereference Operator
💡Pointer Arithmetic
Highlights
Discussion on basics of pointers in C, including the address-of and indirection operators.
Introduction to the concept of pointer to pointer, also known as double pointer.
Explanation of declaring and initializing pointers and pointer to pointer variables.
Visualization of memory allocation and how pointers reference memory addresses.
Clarification on the type of data a pointer can store and the importance of matching data types.
Detailed example illustrating how a pointer to pointer stores the address of another pointer.
Steps to declare and initialize a double pointer, using an example with variables int a, p, and q.
Explanation of accessing values through different levels of pointers, using indirection operators.
Demonstration of how to access and print variable values using single, double, and triple pointers.
Examples showing the errors and warnings that occur when incompatible types are assigned to pointers.
Introduction of a triple pointer and explanation of how it stores the address of a double pointer.
Steps to declare, initialize, and access values using a triple pointer.
Explanation of how to update variable values using single, double, and triple pointers.
Demonstration of printing memory addresses and values using pointers in different levels.
Encouragement to practice with double and triple pointers to understand errors and proper usage.
Transcripts
so in the series of learning programming
in c we are discussing pointers in C so
far we have discussed some Basics about
pointers and those two operator like
address of and indirection operator in
pointer as well as how to assign a
pointer value to another pointer that we
have discussed now in this video we'll
see an extended concept of pointer that
is pointer to pointer or in other word
you can also say it's a double pointer
so this concept is also very important
when you deal with pointers right we'll
discuss this with proper examp example
we'll see a simple program also here we
will try run that program here how like
that program works everything we'll
discuss and then I'll show you
practically on my laptop using vs code
editor right see before going to the
pointer to pointer concept or the double
pointer concept let me just recap this
thing if I if I want to declare a
variable like int a is equal to suppose
10 right and if you want to declare a
pointer then in Star p and pointer is
going to to contain address of any other
variable so I'm going to initialize this
here only address of a right int SRI p
means it means see in memory if suppose
in memory this is our
memory you can draw it horizontally or
vertically it's up to you right and it's
like you can say it's a long tape of
bites something like
this right now these are like one by 1
by one by this kind of thing so in a is
equal to 10 so somewhere memory would be
to this a that is 4 by maybe suppose we
have this this memory something like
this and these four bytes from here 1 2
3 4 these four bytes has been allocated
to this a right and here I'm going to
store
10 like in 32 bits 10 would be you know
this would be converted into binary
digit like 0 and one in 32 bits and that
would be stored here right so now this
is for a and int star p is estri means
obviously this would be a pointer this
is Declaration of pointer or as well as
initialization of this pointer so it
will tell the compiler that P would be a
pointer but obviously memory would be
allocated to this what is size of
pointer on a 32-bit machine it it would
take four byte right so suppose here we
have pointer these four by one 2 3
4 1 2 3 4 these four bites would be for
this P here we have p and here what
would be stored address of a and suppose
address of a is this this bite this
address of a is suppose 1,000 1 th1
1,2,3 so obviously we'll take Base
address like the address of the first
bite of this first address of the first
bite that would be address of this a so
now this th000 would be stored here
right so this would be pointing to this
memory location right so simp for
Simplicity purpose I'm just drawing this
thing like this here we have
a somewhere in memory and location is
address is th000 in a we have 10 right
and one pointer we
have p and suppose address of this is uh
maybe I don't know what should be the
address or I can say like
850 maybe I mean address would be in
hexadecimal form obviously right so now
in P what we have address of a address
of pH th000 so in P we have 1,000 so now
p is pointing to this a something like
this this would be the visual
representation of this this thing right
so now p is what a pointer to integer
data type
right type of this is int like if I
write here type of this is like int star
this thing you can say because this is a
pointer so I think this thing is clear
to you because this is going to be very
important whatever data type you write
here in the Declaration of this pointer
this will
tell this pointer is going to store or
this pointer is going to point which
data type of variable right means here p
is going to store address of a variable
whose data type is integer should be
integer if your data type is float that
would be illegal right I hope you got
this now now what is pointer to
pointer pointer two pointer means see
this is pointer to
variable right pointer to variable it
means this pointer is going to store
address of this variable if I say
pointer to
pointer
means a point pointer to pointer is or
you can say double pointer is a special
variable or special pointer which is
going to store address of another
pointer right not any ordinary or normal
variable this double point is going to
store address of other pointer variable
so if I take here suppose Q I'm taking Q
is another pointer and is Q is going to
store it is pointer to pointer means it
is pointer to suppose this
pointer right it is not pointer to a
normal variable it is pointer to pointer
that is why it is double pointer right
it is going to show
level right so now here 1,000 so not
th000 here address of P would be stored
now address of this pointer is 850 so
here it should be 850 and obviously it
is a variable so it is also having some
memory location suppose memory location
is
1046
right so now Q is here double
pointer Q is pointer to a
pointer to variable so now how to
declare this Q
see
in if I want to declare q and in Q I'm
going to store address of obviously P so
now first
thing Q is also a pointer right and S
will tell the compiler in in the
Declaration estri will tell the compiler
that it is a pointer so definitely one
estri would tell that Q would be a
pointer right now Q is going to store
address of a pointer variable so here
data type which type of data type you
will use whose address this Q is going
to store so Q is going to store address
of this
P so here data type what you will write
int SRI int
SRI that is why here we are having two
estri in double pointer I hope you got
this concept why double estri here right
here estri will tell it is single level
pointer estri will tell this this is a
pointer and int will tell data type of
this variable so here this estc will
tell that Q is pointer obviously Q is a
pointer because it is storing address
but it is storing address of a pointer
variable so a data type you can write
here we have have to tell here the type
which type of address it is going to
store so that should be in
Star right so I hope you got this thing
or you can say it is having what two
level
pointer right so now what you can say
how you can initialize it it will
contain address of another pointer that
is
p if you write here address of
a it would be illegal right it will give
some warning message
incompatible know pointer type because a
is what normal variable and it is double
pointer so according to the rule it
should contain address of a pointer
variable only right so here we must
write any pointer variable that is p now
this is correct I hope you got now what
is double pointer right now see if you
want to print value of
a how you can print a is equal to I'm
going to print you using three methods
so three percentage I'm writing here
either you can write here
what here I'm writing a so that will
also give 10 or you can access it using
this pointer also how you can write
estri
P estri IND Direction operator here it
will act as IND Direction operator but
here it is not IND Direction operator
right because it is Declaration of
pointer so here we have already declared
after that if you use this one this
thing then it will act as Direction
operator or D referencing operator so
value at this address right it it will
also give 10 but if you want to access
using this Q using this double pointer
then how you will access C if I
write s
q it means estri q estri q means Q is
what having address of p in Q we have
850 that is address of P so so it means
this will tell value at this address and
address of p is
850 so it will give value at 850 value
at 850 is
1,000 but that that's not we want we
want value of
a so we want value at th000 this is
again address so again put ack here at
that time it will give 10 value at 1,000
would be 10 so here means it will we
will
use two in Direction operator because it
is obviously it is of it is you can say
it's of two level pointer so for
accessing this value firstly we will go
here then we will go here so one estri
we will use then we go here another
estri we use then we'll go here right so
see here one more estri we have here one
morec we have so value at 850 is
1,000 one more stric value at th000 is
10 so finally it will give 10 so using
double pointer also you can access this
a I hope now you got the concept of
double pointer and same you can increase
this level
also right if I want to uh know store
address of the
skew suppose I'm taking one more
variable uh PQ
R and here I want to store address of
this q that is 1046
so it should point to here and suppose
address of this is
2046 so now Q is what pointer to pointer
two pointer two variable it is having
three level 1 2 and
3 right it means how you will declare
this
in so now obviously R is a pointer
variable because it is any address so
this 1 s trick will tell the compiler
that it is a it is what pointer type of
variable whose address it will
contain Q so Q type in general if you
write like this would be
int
estri
estri right because it is also
containing this address address of a
pointer and then variable so here we
will write two s one and again one so
it's of three level so here we can store
address of
Q if you want to store her address of
P that would also be
illegal right because 1 2 three three
level pointer or in short in shortcut or
in simple term you can say it is three
level pointer so it can store according
to the rule address of two level pointer
two level pointer is Q so according to
the rule it should store address of Q if
a two level pointer it should store
address of one level pointer if a one
level pointer it should store address of
zero level because a is what obviously
zero level having zero level it is
having one level it is two level it is
three
level
right so I hope you got this and using R
if you want to access a then how you can
access 1 2 3 and R here you can write
this thing see how you will get this
thing SRI SRI estrick and R is what
address of Q address of Q is
850 first of all this one then this one
and then this you can write this also
but better to write here the level in
Brackets this and this it's a good
practice what one bracket something like
this right this is also fine without any
brackets so first this star value it
means it will give value at this address
whatever the address return to the next
of this star so estri estri and what
this will give value at 850 value at
850 value at 850 is oh not 850 see R is
what address of Q address of Q is 10
46 right in R we have 1046 so value at
1046 is 850 so this will give
what
850 now this SRI value at 850 value at
850 is 1,000 so it will
give 1,000 so now this s value at th000
is 10 so it will give 10 so finally
answer would be 10 this is how you can
access the variable using three level
pointer right and if you want to store
if you want to you know change this
value using these double pointer and
this
pointer then how you can write if you
write something like
this if I write here estri estri Q is
equal to
25 and now I want to print value of a
then what should be the answer C estri
and EST Q it means what you are
accessing this 25 would be stored where
the s q means value at Q in Q we are
having 850 so value at 850 is value at
850 is 1,000 then again here would be
th000 but this star we also have then
value at th000 is 10 so ultimately we
are accessing this and here we are going
to show 25 so if you want to print a now
then it would be
25 right but but see here I'm writing
here I'm writing this is question for
you rather than this I'm just writing s
q is equal to 25 what would be the
answer if you will print a then what
would be the answer or where this 25
would be stored or it it will give any
error or it is illegal or not that you
have to tell me in comment box right I
think it it is illegal it will give some
error why so see EST Q means value at q
q is what Q is containing 850 so SRI 850
Q is what pointer to pointer so when
value at
850 value at 850 is 1,000 and here I
want to store
25 but here it is containing
address so directly we cannot assign any
integer value here in any pointer this
would be invalid incompatible type of
error it will give right because the
here the DAT type is or you can say here
this type of this is what in Star but
you are going to assign integer only
that would be illegal
right so it will give error fine now see
you have to assign this Q value using
this R pointer that you have to do right
you have to change this value to
50 but using this pointer R how you will
change it and how you will print that
thing you have to tell me in comment box
right so I guess this is not very tough
you got the concept pointer two pointer
or double pointer and why we are writing
this this double pointer and using
double pointer also how you will access
the variable value of that
variable right so now let me show you
practically this thing so now let me
create a file
here I'm taking
name double pointer do c
right header file and then we'll write
main function and here I'm going to take
a variable in a is equal to 10 and here
I'm taking a what single pointer first
of all right in single pointer we can
store address of a normal variable right
address of a and here I want I'm
declaring double pointer suppose q and
in double pointer I'm initializing we
can store according to the rule address
of any pointer variable so here we have
P pointer variable right so now uh
just
print a a is equal to percentage
D and
uh we are printing value of a
here using simple a or another method is
you can also print a is equal to
percentage D and using pointer single
pointer so estri
P that's it right and one more more
method is obviously we can also
access this variable using double
pointer also right it's a chain of
pointer right print
F or you can say it's
like of a right obviously if you have
relative it's D relative but then also
anyone who knows your D relatives then
he or she can also access you through
those dur relatives right I hope hope
you got my point so now a is equal to
because you know these are Interlink
pointer two pointer then two variable so
percentage D and here how to access
estri
estri and
Q
right now let me print
this what output it will give give C in
three cases a is 10 it is is giving
value of pH 10 so using double pointer
also this is how you can access this a
or if I write here see if I write here
in s p is equal to um address of a I'm
going to store address of a single
simple variable normal variable and here
let me just run this and see what
happens see initialization of in star
star it's like incompatible pointer type
int Star right so it will give this type
of error and it has stopped working so
according to the rule we shouldn't do
this
right and see it is giving a 10 and a 10
these two a and star B so that is not
correct if a two Lev pointer it it can
only store address of a one level
pointer that is address of P right and
here if I'm taking int three level
pointer star star star
r that according to the rule should
contain address so for two level pointer
here two level pointer is Q because it
is having two estri
right so that's it and using this also
we can also access
a a is equal to percentage D and uh here
I'm accessing how three SRI sign we have
to use right because it is three level
pointer srick and here I'm using what
R so this also will give you same result
that is 10 see here a is equal to 10
fine
now another another thing what we can do
is see
here if you want to print if you want to
print address of
Q you want to print address of Q so
address of Q is where in which pointer
in r r is storing address of Q so if you
print here print
F address of
Q in hexadecimal form I want to print so
percentage uh X right and simply you can
print R and that will give address of Q
or if you want to print address of Q
then you can also write address of
operator and address of Q so here I'm
writing two percentage x two times and
that that should give same result right
and suppose you want to print address of
address of P now address of p is where
in Q right
so address of p is equal to address
would be in exod decimal form and using
two know method I want to print one is
what address of Simply use address of
operator so address of P it will give
address of P or next thing is address of
p is where in Q pointer Q is double
pointer so simply you can print Q
here that will also give address of P
right so now let me save this and let me
run this now what it will give
see okay let me uh do what
here it should be SL then you will get
it better right see
now address of Q is both are giving same
result address of Q is either you can
print r or address of Q obviously
address would be same so it will give
same result address of PS both are
giving same 61 FC C4 fc4 right so this
is the method either you can print
address of P using address of p and or Q
right address of R if you want toint
then then address of RS where is stored
no we have no pointer which is going to
store address of Q we haven't declared
any pointer if you want to declare you
can see like star star star star another
pointer maybe you can say and take s or
t or there you can store address of r
four level pointer right or simply you
can print here address of R using
address of operator right and you can
also see something like you can also do
something like this if I write here star
p is equal to
2 right and after that I'm writing star
star Q is equal to
17 so according to what should be the
final value of a see SRI p means value
at p in P we have what address of a so
value at that address is 10 so in a 12
would be stored but again estri SRI Q
this is also another way what to
access value of a so now value of a
becomes 17 so final value of a becomes
17 so it should print 17 see
now let me save it and again run it see
now value of a 17 I hope you go we can
update this thing or using this operator
Al so like s star star star R and if I
write here 78 now final value of a is 78
because this also meaning of this is
also same thing we are accessing value
of a right I hope this is clear double
pointer and you know level of
pointers so you can also know practice
these kind of simple simple problems
using these double pointer and triple
pointer and this kind of thing and you
can print uh try to print values and
variables and you can see what kind of
error you are getting right so that's it
for this video now so in the next video
we'll see some another concept of
pointers now I'll see you in the next
video till then bye-bye take care
Weitere ähnliche Videos ansehen
C_73 Pointers in C- part 3 | Pointer Assignment
C_82 What is Dangling pointer in C | C Language Tutorials
Understanding and Using Function Pointers in C
you will never ask about pointers again after watching this video
C_80 Void Pointer in C | Detailed explanation with program
Understanding the Null Pointers
5.0 / 5 (0 votes)