Hello, and welcome back.
I want to talk a little bit about void pointers in this lecture.
We know what void is because we've used this keyword
when we've talked about functions.
Mainly if we don't want to return any data from a function, we use the void
as part of the function signature. What about when we use avoid pointer, what does this mean?
Well, the type name void means absence of any type.
So a pointer of type void star can contain the address of a data item of any type,
so it has a lot of flexibility.
If you don't know what you're going to store as a pointer, you can say void star,
pretty nifty. It's usually used in functions for function parameters.
So void star is often used as a parameter type or a return type
with functions,
that deal with data in a type independent way.
So you can allow your function to be more flexible
if it takes void star pointers.
It basically means that
your function can take more than one type of variable
except it's not completely flexible because you have to cast it some way when you dereference it.
So any kind of pointer can be passed around as a value of type void, any type.
The void pointer does not know what type of object it is pointing to.
So you cannot dereference it directly,
and that's where the real hiccup comes along with the void star.
It's great for long flexibility for different data types,
but you can only use it if you know the data type.
So you have to provide an explicit cast
to the pointer when you dereference it.
The address of a variable of type int can be stored in a pointer variable of type void
because void star means that any type of data can be stored inside there.
So when you want to access the interval value
at the address stored in the void star pointer, you have to first cast it
to a pointer of type int.
In this example here is we're going to see how you use the void star pointer.
We have three variables we defined, an integer and a float and a character.
We're now going to create a void star pointer.
Because the pointer is of type void, we can assign it
the address of i which is an int. We can assign it the address of f,
which is a float and we can assign it the address of character
or ch which is a character.
The only problem is when you dereference, you have to know that type,but you still see the advantage here.
The advantage here is we only have to create one variable type
and we can store multiple different addresses in it.
But whenever you want to use it, you have to use that in star.
And you'll see here when we're printing out the data, that the in star
is casting the pointer and then we're de-referencing it, the star outside the int
parentheses is dereferencing. So that's going to print out the value
that v pointer is pointing to,
but first it's going to cast it to the right type.
So in all three of these v pointers, it's going to print out for the ant it's going to print out 10.
For the float, it's going to bring out 2.34.
And for the ch, char, it's going to print out k.
We'll see the void pointer used more
in function parameters, like I said, in return types.
And that's really the value when it comes into and as well as
only using a void star pointer
in one declaration and assigning many different data types. Thank you.