C_80 Void Pointer in C | Detailed explanation with program
Summary
TLDRThis video script delves into the concept of void pointers in C programming. It explains that a void pointer, akin to an empty glass that can be filled with any liquid, is a generic pointer without an associated data type. The script teaches how to declare and use void pointers, emphasizing the need for type casting when dereferencing to access values of different data types. The practical demonstration shows using a single void pointer to access variables of various types, highlighting its utility in simplifying code and handling dynamic memory allocation.
Takeaways
- π The video discusses special pointers in C programming, focusing on void pointers.
- π A void pointer in C is a generic pointer that has no associated data type, similar to an empty glass that can be filled with any liquid.
- π The term 'void' generally means 'empty' or 'nothing', and a void pointer is used to point to any data type.
- π‘ Declaring a void pointer involves using the 'void' keyword in the pointer declaration, such as `void *vp;`.
- π« A void pointer cannot be dereferenced directly; it must be typecast to a specific data type before being used.
- π The flexibility of a void pointer allows it to be converted into any other pointer type, making it useful for various programming scenarios.
- π» The video demonstrates how to use a void pointer in a program, showing how it can store the address of different data types like int, float, and char.
- π Typecasting a void pointer is essential for accessing the value it points to, as the compiler needs to know the data type to correctly interpret the memory.
- π The practical use of void pointers is highlighted in the video, showing how they can be used to access multiple data types with a single pointer.
- π The video also mentions that void pointers are used in dynamic memory allocation functions like malloc and calloc, which return void pointers to the allocated memory.
Q & A
What is a void pointer in C?
-A void pointer in C is a pointer that has no associated data type. It is essentially a generic pointer that can point to any data type. It is declared using the 'void' keyword and can be typecast to any other pointer type.
Why are void pointers useful in programming?
-Void pointers are useful because they can be used as a generic pointer to any data type. This allows a single pointer to be used to access different data types, which can be convenient in situations like dynamic memory allocation where the data type is not known at compile time.
How do you declare a void pointer in C?
-A void pointer is declared in C using the 'void' keyword followed by an asterisk and the pointer name. For example, `void *vp;` declares a void pointer named 'vp'.
Can you directly dereference a void pointer?
-No, you cannot directly dereference a void pointer. Before dereferencing, you must typecast the void pointer to the appropriate data type pointer (e.g., `int *`, `float *`, `char *`) to ensure the compiler knows how many bytes to access.
What is the significance of the term 'void' in the context of a void pointer?
-The term 'void' in the context of a void pointer signifies that the pointer does not have an associated data type. It can be thought of as an 'empty' or 'generic' pointer that can be filled with any type of data.
How does a void pointer differ from other pointers like integer pointer, float pointer, or character pointer?
-A void pointer differs from other pointers in that it does not have a specific data type associated with it. Other pointers, such as integer pointers (`int *`), float pointers (`float *`), or character pointers (`char *`), are associated with specific data types and can only point to variables of those types.
What is the purpose of typecasting a void pointer?
-Typecasting a void pointer is necessary to specify the data type of the variable it points to. This is required before dereferencing the pointer so that the compiler knows how many bytes to access and can correctly interpret the data.
Can a void pointer store the address of any variable?
-Yes, a void pointer can store the address of any variable, regardless of its data type. However, before accessing the data at that address, the void pointer must be typecast to the appropriate pointer type corresponding to the data type of the variable.
What are some common uses of void pointers in C?
-Void pointers are commonly used in situations where the data type is not known until runtime, such as in dynamic memory allocation with functions like `malloc` and `calloc`. They are also used in generic functions or APIs where the function needs to handle data of various types.
How does the concept of a void pointer relate to dynamic memory allocation in C?
-In C, functions like `malloc` and `calloc` return a void pointer. This void pointer is then typecast to the appropriate pointer type by the programmer to store and access dynamically allocated memory. This allows for flexible memory management where the data type can be specified at runtime.
Outlines
π Introduction to Special Pointers in C
This paragraph introduces the concept of special pointers in C programming. The speaker discusses various types of pointers such as word pointer, generic pointer, null pointer, and dangling pointer. The focus is on void pointers, explaining what they are, their purpose, and how they are used. The void pointer is described as a type of pointer that does not have an associated data type, making it versatile for various uses. The speaker also mentions that they will demonstrate the use of void pointers with practical examples on their laptop.
π Understanding and Using Void Pointers
The speaker delves deeper into void pointers, explaining that they are essentially pointers without a specific data type. They use the analogy of an empty glass that can be filled with any liquid to illustrate the concept. The paragraph covers how to declare a void pointer and how it can be converted into any other type of pointer through type casting. The speaker emphasizes the need for type casting when dereferencing a void pointer, as the compiler needs to know the data type to correctly access the memory. An example is given where the void pointer is assigned the address of an integer variable, and the speaker explains the error that occurs when trying to dereference it without type casting.
π Type Casting and Practical Use of Void Pointers
This paragraph discusses the practical applications of void pointers, highlighting their utility in accessing different data types with a single pointer. The speaker explains that while void pointers do not have an associated data type, they can be type cast into any other pointer type, allowing for flexibility in accessing various data types. Examples are provided where the void pointer is used to access integer, float, and character variables, demonstrating how a single void pointer can be used to handle multiple data types. The speaker also mentions the use of void pointers in dynamic memory allocation functions like malloc and calloc, which return void pointers.
π» Demonstrating Void Pointers in Code
The speaker presents a practical demonstration of using void pointers in C code. They create a file named 'void pointer do c' and declare a single void pointer. The code example shows how the void pointer is used to access variables of different data types (integer, float, and character) by first storing their addresses and then dereferencing the pointer after type casting. The speaker runs the code to show that the values of the variables can be accessed correctly using the void pointer, emphasizing the benefit of using a single pointer to handle multiple data types.
Mindmap
Keywords
π‘Pointers
π‘Void Pointer
π‘Data Type
π‘Type Casting
π‘Dangling Pointer
π‘Null Pointer
π‘Generic Pointer
π‘Memory Address
π‘Dereferencing
π‘Dynamic Memory Allocation
π‘Compiler
Highlights
Introduction to special pointers in C programming, including void pointer, word pointer, generic pointer, null pointer, and dangling pointer.
Void pointer is a generic pointer type that can point to any data type.
Void pointer has no associated data type, making it versatile for various uses.
Void pointer can be converted into any other pointer type through type casting.
Declaration of a void pointer involves using the 'void' keyword.
Demonstration of how to declare and use a void pointer in a program.
Explanation of why void pointers cannot be dereferenced directly.
Void pointers require type casting before dereferencing to specify the data type.
Illustration of accessing an integer variable using a void pointer by type casting it to an integer pointer.
Example of accessing a float variable using a void pointer by type casting it to a float pointer.
Demonstration of accessing a character variable using a void pointer by type casting it to a character pointer.
Void pointers are used in dynamic memory allocation functions like malloc and calloc.
Practical example of using a void pointer to access variables of different data types in a single program.
Void pointers allow for accessing multiple data types with a single pointer, reducing the need for multiple pointers.
Explanation of the practical applications and benefits of using void pointers in programming.
Void pointers are essential in scenarios where the data type is unknown or needs to be flexible.
Final demonstration of accessing and printing values of different data types using a single void pointer in a C program.
Transcripts
so in the series of learning programming
in c we are discussing pointers in C in
pointers also there are some special
pointers like word pointer generic
pointer null pointer dangling pointer
and these pointers are also very helpful
right so we are going to discuss these
pointers one by one in this video I'll
discuss void pointer everything about
void pointer like what is void pointer
the need of void pointer why we use void
pointer how to declare this pointer how
to use this pointer right with the help
of a program right and I'll show you
that program also on my laptop practical
also we'll see right everything we'll
discuss about this pointer in this video
and one by one by one in later videos
you will discuss dangling pointer null
pointer all the these special pointers
we'll discuss right so you'll also see
how this pointer is different from the
other pointers we have discussed like
integer pointer character pointer float
pointer everything about this pointer we
will discuss in this video every single
detail right so now let's discuss what
is void pointer I hope you know the
meaning of this void in general term in
English term like K or empty right so
it's like if there's a glass and that is
empty that is void you can fill that
glass with maybe water with some other
drink or with milk anything you can you
know put in that glass right so apply
the same concept here also try to relate
that thing void pointer means this
pointer is of void type means this
pointer is having no Associated data
type if you will see the definition it
that maybe on internet or in any book
then you must you must uh see that word
pointer is what it is not having any
Associated data type that is in simple
terms that is a the same example you can
take an empty glass you can fill that
glass with either you know with water or
with milk or with some other drink or
anything right so same this pointer is
having no Associated data type with it
so you can convert this pointer in any
other pointer also now whatever I am
saying what's the meaning of that thing
see first of all if I write a pointer
something like this int
star
P one is float star FP here I'm writing
IP or here I'm writing
CP it means there are three pointers
this is integer pointer see data type of
pointer is not integer it means IP is a
pointer which is pointing to
a int data type or maybe this is storing
an address of a variable whose data type
is integer or simply we call it as in
integer pointer or pointer to integer
this is float pointer or you can say a
pointer to float this is pointer to
character right so they they are having
their Associated data type the data type
of this is the data type which is
associated with this pointer is int with
this float with this care but word
pointer is having no Associated data
type so how to declare this pointer see
the synx is same obviously if it is a
pointer you have to use EST this will
tell that it is a pointer name here you
will write pointer name suppose I'm
writing pointer name word pointer that
is WEP so what you will write here at
the place of this data type simply we
will write what this void this keyword
void so this this is what a declaration
of void pointer right I hope you
understand what is void pointer it is it
it is not having any Associated data
type right so you can convert it into
any other data type you can type cast it
right here it means this can store
address of a variable like if a address
variable is in a another variable I'm
taking float B one variable I'm taking
care
C so this pointer can store IP is equal
to address of
a only this is possible this can store
address of a only if you write IP is
equal to address of B this would be
wrong and what if we can write FP is
equal to address of B and this pointer
CP is equal to address of
C right but here this pointer can store
address of
this int variable also float variable
also care variable
also right so it's like a generic
pointer same in Empty Glass we can fill
maybe water some other drink or maybe
milk anything we can typ cast it so we
can typ cast this pointer for that you
have to typ cast this pointer directly
if you write suppose here I'm writing
VP is equal to address of
a it means now this word pointer is
storing address of
this variable a and suppose in a I'm
having a is equal to 5 but if you will
print like print F percentage D and
whatever in a I want to print in a I
have five and this WEP this
pointer this is pointer
this is pointing to a suppose address is
th000 so in VP we have address of a that
is
1,000 so if you want to access this
using this wordid pointer what you will
write D reference you will D reference
this pointer I hope you know this
because we have discussed these Concepts
I know uh in our previous videos of
pointers right it means estri VP value
at this address here we have th so so it
should give
five but it is wrong you cannot
dreference aoid pointer it will give you
error first of all you have to typ cast
it because see when you deference then
it will see suppose I'm D referencing
this IP I'm writing here IP IP is equal
to address of a suppose this is not VP
this is IP this pointer right
and EST IP so it will give you five this
is correct because this is integer
pointer this is not word pointer so now
when you dreference it it means compiler
now know add the data type of this
pointer the associated data type with
this pointer is integer means now
compiler know in modern compiler
obviously it depends on machine to
machine but I take like in modern
compiler integer take four bytes so now
compiler
[Music]
know 1 2 3 4 suppose in memory within
these four bytes this a the value has
been stored five converted into binary
form like 32 bits and then this five has
been stored here and address of first
bite is 1,000 then 1,1 1, and2 1,
and3 something like this right so
now value at this address address is
th000 address is th000 so now from here
we want to access now Associated data
type is integer so when you apply D
referencing now the compiler know
that he has to access four bytes 1 2 34
because data type is
integer that is why the data type is
needed when you dreference that pointer
right so now it will access four
bytes and it will give you
value same if you access D reference
this character pointer character will
take one bite so now compiler know by
seeing this data type compiler will know
that he has to access only one by
because dat type is
character right same float if float is
there means four bytes so it has to
access four bytes but here we are using
suppose
VP now word is what it is not a data
type now how compiler will know
that it has to access one byte or 4 byte
or how many bytes compiler doesn't know
so how to access this void pointer how
to dreference this void pointer how to
get the
value right value may be within one by
or value may be in four bytes don't know
it is void pointer right that is why we
we cannot dreference this word pointer I
hope you got the reason why you cannot
dreference this word pointer right if
not then you can ask me in comment box
maybe in maybe in another video I can
tell you the process in a separate video
right so now that is why it will give
error so first of all we will have to
typ cast this
one typ cast means now I'm storing in VP
address of a so here we have VP I'm
storing address of a and data type of
this is what integer so we have to typ
cast it into integer pointer so now how
to type cast it here rather than using
this what you will
write int
estri VP so first this word pointer
would be typ casted or you can say
converted into an integer pointer and
now we apply this D referencing
operator right now it will give you what
value
five because now now we have converted
into int or we you can say typ casted
into integer pointer so now compiler
know if int then it has to access four
bytes so data would be in four bytes so
starting by is this so it will access
1,00,000
1,2,3 till 103 4 bytes and obviously
this is integer type and this would be
stored in four bytes only so definitely
you will get five
right now if I write if I write
something like this VP is equal to
address
of CP uh sorry address of uh
C I'm storing VP this address variable
is what character type right so here in
C suppose I'm storing character is uh
zero yeah 0 is character it's not like
that AB BCD is only character whatever
you will write in single code that will
be in character A Single Character you
can write so if you write zero here so
zero would be considered as a character
right so now address of C now VP is
suppose here we have this is C here we
have zero obviously in that form like in
uh Zer and one form that would be stored
right and now VP this is a pointer and
this is pointing to here suppose address
is 2,000 so it we are storing 2000
now now how to
print how to print this is character we
know this is character so we'll use
percentage C estri first type
cast type cast into a character pointer
so we'll write here care s and now name
is
VP that's it first of all it will be typ
casted into character pointer and then
we'll der reference it so D reference
means value at this point value is what
zero so it will give zero I hope you got
this and if you write here VP is equal
to address of float variable so before
printing type cast into float and that
it will print floating value suppose in
B I have 1.11 so it will print
1.11
right so simple point about word pointer
there is nothing much about word pointer
it is not having an data type you can
convert it into any other pointer or you
can typ cast it into any other pointer
right so now what's the use maybe you
think that rather than typ casting
obviously ultimately we have to typ cast
it so rather than this directly use an
INT pointer and directly deference that
pointer what's the use of using this
void pointer so here if in the same
program same program if I'm using this
int also float also so care
also so we have to declare three
pointers one is for INT one is for float
one is for
character but if you use this word
pointer no need to declare these
pointers you can access all these three
values using single pointer word pointer
only a single pointer how I know I I I
hope you know the
answer first store the address of a type
card it and print then assign in this
address of B while printing type cast it
then assign address of C while printing
type cast it and you can print you can
access all the three values three values
of different different data
types so here we are using only one
pointer we are using only one pointer
and we are accessing values of three you
can say data types integer also float
also car also one more thing when we use
Melo and
kello we'll also discuss these
things these are built-in function for
dynamic memory allocation so they return
what void
pointer right when they assign they
return memory like they return word
pointer to that memory so in that memory
we can store any uh value like integer
also float also character also right so
there also these void pointers would be
used right in Malo and
kog so that also discuss when we discuss
dynamic memory location these are
Advanced you know Concepts and see right
so that is why we use I guess uh word
pointer you you are clear with what is
word pointer why we use word pointer
like and you can say what's the need of
word pointer now let me show you know
practically we'll write down some code
and we'll see use of word pointer right
so now let me create a file here
I'm taking name void pointer do c
right and I'm taking only one pointer
like void estri
VP right and I'm taking in a is equal to
5 and one more thing that is float
suppose I'm taking float B is equal to
1.56 and
care care C at H is equal to uh anything
you can take like I'm taking
C right and now I'll access all the
three variables using one pointer only
so I'm first of all I'm writing VP is
equal to address of a and now I want to
dreference
this a is equal to
we are printing value of a and see if
you der reference like this s will be
simple so it will give you error let me
run
this here we it is giving error like d
referencing word pointer and invalid use
of word expression so we cannot this
deference this word pointer something
like this now we have to type cast it so
I'm typ casting it into integer pointer
first
and see now let me run this and it will
print a is equal to
5
see see it is printing the value a is
equal to 5 right now I want to access
that variable float so in VP I'm storing
address of B now so now this word
pointer is
pointing
what float variable so now B value is
equal to percentage
F and here I'm D referencing this now so
we have to convert it into
float we have to type cast it into float
see let me run this and it will give you
a equal to uh sorry this a is equal 5 b
is equal to 1.5
6 right and now same address of
CH and let me print
C = to percentage
C and uh
estri we now we have to type cast into
care
right and now let me print this sorry
run this see it is printing a b c uh CH
value all the three values so we are
accessing all the three data types
variables with a single pointer
only so that is benefit of using word
pointer that is why programmers use this
word pointer right
Browse More Related Video
Understanding the Void Pointers
The What, How, and Why of Void Pointers in C and C++?
C_73 Pointers in C- part 3 | Pointer Assignment
C_81 Null Pointer in C | C Programming Tutorials
C_71 Pointers in C - part 1| Introduction to pointers in C | C Programming Tutorials
C_75 Pointers in C-part 5 | Pointer Arithmetic (Addition) with program
5.0 / 5 (0 votes)