English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
So far, we have studied that the address assigned to the pointer should be the same as the type specified in the pointer declaration. For example, if we declare an int pointer, then this int pointer cannot point to a float variable or some other type of variable, that is, it can only point to variables of the int type. To overcome this problem, we use pointers to void. A pointer to void represents a general-purpose pointer that can point to any data type. We can assign the address of any data type to a void pointer, and we can assign a void pointer to any type of pointer without performing any explicit type conversion.
void *pointer name;
The following gives the declaration of void pointer:
void *ptr;
In the above declaration, 'void' is the type of the pointer, and 'ptr' is the name of the pointer.
Let's look at some examples:
int i=9; //Initialization of integer variable. int *p; // Declaration of integer pointer. float *fp; // Declaration of floating-point pointer. void *ptr; //Declaration of void pointer. p=fp; // Incorrect. fp=&i; // Incorrect ptr=p; // Correct ptr=fp; // Correct ptr=&i; // Correct
The size of void pointers in C language is the same as the size of char type pointers. The representation of pointers to void is the same as that of char type pointers. The size of the pointer will vary depending on the platform used.
Let's see the following example:
#include <stdio.h> int main() { void *ptr = NULL; //Void pointer int *p = NULL;// Int pointer char *cp = NULL;//Char pointer float *fp = NULL;//Float pointer //Size of void pointer printf("Size of void pointer = %d\n\n",sizeof(ptr)); //Size of int pointer printf("Size of int pointer = %d\n\n",sizeof(p)); //Size of char pointer printf("Size of char pointer = %d\n\n",sizeof(cp)); //Size of float pointer printf("Size of float pointer = %d\n\n",sizeof(fp)); return 0; }
Output Result
Size of void pointer = 4 Size of int pointer = 4 Size of char pointer = 4 Size of float pointer = 4
Here are the advantages of void pointers:
The malloc() and calloc() functions return void pointers, so these functions can be used to allocate memory for any data type.
#include <stdio.h> #include<malloc.h> int main() { int a=90; int *x = (int*)malloc(sizeof(int)); x=&a; printf("Value pointed to by x pointer: \t%d",*x); return 0; }
Output Result
Value pointed to by x pointer: 90
Void pointers in C can also be used to implement generic functions in C.
Due to its reusability, we use void pointers. Void pointers can store objects of any type, and we can retrieve objects of any type by using indirect operators with appropriate type conversions.
Let's understand this through an example.
#include<stdio.h> int main() { int a=56; //Initialize an integer variable 'a'. float b=4.5; //Initialization of float variable 'b'. char c='k'; //Initialization of character variable 'c'. void *ptr; //Declare an empty pointer. // Allocate address for variable 'a'. ptr=&a; printf("value of a is: \t%d",*((int*); //Allocate address for variable 'b'. ptr=&b; printf("\nvalue of b is: \t%f",*((float*); //Allocate address for variable 'c'. ptr=&c; printf("\n The value of c is: %c",*((char*); return 0; }
Output Result
The value of a is: 56 The value of b is: 4.500000 The value of c is: k