It is a pointer that can hold the address of any datatype variable (or) can point to any datatype variable.
Declaration
The declaration for void pointer is as follows −
void *pointername;
For example − void *vp;
Accessing − Type cast operator is used for accessing the value of a variable through its pointer.
Syntax
The syntax for void pointer is given below −
* ( (type cast) void pointer)
Example 1
int i=10; void *vp; vp = &i; printf ("%d", * ((int*) vp)); // int * type cast
Example
Following is the C program for void pointer −
#include<stdio.h> main ( ){ int i =10; float f = 5.34; void *vp; vp = &i; printf ("i = %d", * ((int*)vp)); vp = &f; printf ( "f = %f", * ((float*) vp)); }
Output
When the above program is executed, it produces the following result −
i = 10 f = 5.34
Example 2
Given below is the C program for pointer arithmetic in void pointers −
#include<stdio.h> #define MAX 20 int main(){ int array[5] = {12, 19, 25, 34, 46}, i; void *vp = array; for(i = 0; i < 5; i++){ printf("array[%d] = %d\n", i, *( (int *)vp + i ) ); } return 0; }
Output
When the above program is executed, it produces the following result −
array[0] = 12 array[1] = 19 array[2] = 25 array[3] = 34 array[4] = 46