Void pointer definition
Void pointer refers to a special type of pointer that programmers can point to any type of variable. It’s also known as a generic pointer. Its main characteristic is that they have no associated data type.
However, a void pointer cannot be dereferenced directly, unlike other pointers. This is because the compiler doesn’t know the data type of the variable that the pointer is referencing, so it doesn’t know the size of the data it would need to access. To use the value that a void pointer points to, you first have to cast the void pointer, or tell it what kind of an item it is pointing to.
Where void pointer is used
- Generic functions. They can help create functions that can handle multiple data types such as a function to sort elements. Instead of writing a function for each data type, you can use void pointers that sorts integers, floats, and characters.
- Generic data structures. They can be used to create data structures that can store any data type.
- Function pointers. They can be used when you don’t know or don’t want to specify the arguments types of a function pointer.