Ad Code

6/recent/ticker-posts

Void Pointers | Data structure

Void Pointers

Before moving toward the advance topics of Data Structure lets understand first some simple topics like void pointers.

Check previous topics here 

What are the void pointers ?

the pointer which has no associated data type with it is known as void pointer. The best thing of this pointer is it can point to any data no matter which data type is associated with that data. According to our need we can typecast to any type.

Take on example for better understanding  ,

Here we have one variable with assigned value 10. Then in next line we have a pointer which is a void pointer. Observe it carefully it has no data type associated wit it. Then moving further we are printing the value by using printf function. You can clearly see that the pointer typecasted first and then deference is happened. typecasting means simply we are changing the type of a pointer.

Note :- We can't deference the void pointer. For deferencing the void pointer we have to typecaste it first.

you must have got on question that what is the use of void pointer? Why we are using it?. So the simple reason is malloc and calloc functions returns the void pointers. so that they can allocate memory with any data type 

Post a Comment

0 Comments

Ad Code