The assignment operator (=) may be used on pointers of the same type. However, if the types of pointers (types of variables to which they point) are not same then we will have to do type casting of one of these to the type of the other to use assignment operator. However, the void pointer (void * ) can represent any pointer type. Thus, any type of pointer may be assigned to a void pointer. However, the reverse is not valid. A void pointer cannot be assigned to any other type of pointer without first converting the void pointer to that type.
However, while dereferencing a void pointer it has to be type cast because a void pointer is a pointer without a type. The compiler does not know the number of bytes occupied by the variable to which the void pointer is pointing to. Therefore, void pointers have to be cast to appropriate type before assigning. Program provides an illustration of void pointers.
#include <stdio.h> void main() { int n = 30; int* ptrn = &n; float y = 10.7, *ptry= &y; void *Pv ; //declaration of void pointers Pv = ptrn; //assigning ptrn to pv clrscr(); printf("*(int*)Pv = %d\n", *(int*)Pv); //dereferencing pv Pv = ptry; //assigning ptry to pv printf("*(float*)Pv= %f\n", *(float*)Pv);//dereferencing pv }