What is Void Pointers in C :
- In C General Purpose Pointer is Called as void Pointer.
- It does not have any data type associated with it
- It can store address of any type of variable
- A void pointer is a C convention for a raw address.
- The compiler has no idea what type of object a void Pointer “really points to.” ?
Declaration :
void * pointer_name ;
Live Example :
void *ptr; // ptr is declared as Void pointer char cnum; int inum; float fnum; ptr = &cnum; // ptr has address of character data ptr = &inum; // ptr has address of integer data ptr = &fnum; // ptr has address of float data
Explanation :
void *ptr;
- Void pointer declaration is shown above.
- We have declared 3 variables of integer,character and float type.
- When we assign address of integer to the void pointer, pointer will become Integer Pointer.
- When we assign address of Character Data type to void pointer it will become Character Pointer.
- Similarly we can assign address of any data type to the void pointer.
- It is capable of storing address of any data type