What is void pointer explain with example
Void Pointer
A variable declared as a pointer is not just a pointer type variable. It is also a pointer to a specific fundamental data type, such as a character.
A pointer therefore always has a type associated with it. We cannot assign a pointer of one type to a pointer of another type, although both of them have memory addresses as their values. This is known as incompatibility of pointers.
All the pointer variable store memory addresses, which are compatible, but what is incompatible is the underlying data type to which they point to.
we cannot use the assignment operator with the pointers of different types.
We can however make explicit assignment between incompatible pointer types by using cast operator
For example :
int x;
char *p;
p = (char * ) & x
We have an exception. The exception is the void pointer (void *).
The void pointer is a generic pointer that can represent any pointer type.
All pointer types can be assigned to a void pointer and a void pointer can be assigned to any pointer without casting.
A void pointer is created as follows :
void *p;
Since a void pointer has no object type it cannot be derefrenced.
Labels: C