The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type:
Is correct because the address of integer variable is stored in an integer pointer.
If a user writes the statement:
Then this statement produces an error. The address of the float variable is stored in an integer pointer that is incorrect.
Similarly, if the programmer tries to place the address of an integer variable to a float pointer, such as:
The above statement will also show an error.
The Pointer to Void is a special type of pointer that the programmer can use to point to any data type.
Using the above example, the programmer declares pointer to void in this manner:
Using the above example's definition and assigning the pointer to void to the address of an integer variable is perfectly correct.
Using the above example to define the pointer to void and assign the pointer to void to the address of a float variable as below is also perfectly correct.
Pointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any data type. There are limitations in the usage of void pointers that are explained below.
The concept of dereferencing using the operator * has been explained in an earlier section of this tutorial. The programmer must note that void pointers cannot be de-referenced in the same manner. Direct dereferencing of void pointer is not permitted. The programmer must change the pointer to void as any other pointer type that points to valid data types such as, int, char, float and then dereference it. This conversion of pointer to some other valid data type is achieved by using the concept of type-casting (refer to type-casting section of this tutorial).
The concept of NULL pointer is different from the above concept of void pointer. NULL pointer is a type of pointer of any data type and generally takes a value as zero. This is, however, not mandatory. This denotes that NULL pointer does not point to any valid memory address.