Updated April 3, 2023
Definition of C Void Pointer
A void pointer in C is a pointer that does not have any associated data type. A void pointer in C clearly indicates that it is empty and can only capable of holding the addresses of any type. Further, these void pointers with addresses can be typecast into any other type easily. Memory allocation also gets easy with this type of void pointer in C. It makes all these functions flexible for allocating the bytes and memory appropriately. These pointers in C are very helpful in implementing generic functions in C.
Syntax:
void *pointer_name;
The syntax flow follows in a way that keyword void is the type of pointer followed by the name of the pointer which is being pointed and allocated as an address allocation. The Pointer declaration is performed with the pointer name and the pointer type supporting any data type. Representation of pointer in terms of C is the same as the pointer of character type.
Example:
void *ptra
This example shows that the pointer is expecting a void type of pointer and then it is being pointed by the pointer whose name is given as ptra inclusive of ‘*’ symbol which denotes that a pointer is being declared and will be used in mere future for dereferencing purpose.
How does Void Pointer Work in C?
The pointer concept in C is very useful as it helps in memory allocation and address management. It helps in implementing two types of pointers namely void pointers and generic pointers. Therefore, it is sometimes called a general-purpose pointer. Referencing and Dereferencing plays a vital role in pointer concept as well as in void pointer.
Let’s walk through the working of the void pointer in C which is performed using pointers that are not at all associated with any other data type. It contains any data type which will contain the address of the value. a pointer declared with keyword void is a void pointer in C. As mentioned earlier referencing and dereferencing are some of the methods associated with pointer concept which will be used. Dereferencing comes into picture whenever it is a need to access the stored value in the pointer variable. Also, there is a type of casting value which is used for dereferencing because none of the pointer value is associated with the data types. The compiler also cannot find the type of variable which is pointed by any type of void pointer. One point to keep in mind is void pointer will not support any kind of arithmetic operation. It makes use of indirection operator ‘*’ to serve the entire purpose. But to serve this problem there is a need to typecast the pointer variable as well for dereferencing. The usage of typecasting is needed because there is no presence of datatype associated at the time of declaration of the pointer. In short, the compiler doesn’t have any appropriate source to get an idea of the type of data type declared. So, it performs the typecasting and meets the requirement to give an intimation of the type of data type used by the void pointer at the time of declaration.
Size of the void pointer is the next point of focus as a void pointer in C functions almost the same as character pointer in C which means a representation of Character type of pointer will be the same as a void pointer in C. Also, the size will vary according to the platform being used by the pointer. Memory allocation also works in some format which means void pointer has the beauty of providing an enhancement feature of memory management with calloc () and malloc () functions which ultimately returns the void as return type. Therefore, these functions can be used to allocate the memory of any data type.
The most important theme line of all the added advantage which pointer has is that it has the power of reusability for void pointers. It can store any type of object and can retrieve any type of object from the defined object using the indirection operator and operator with proper typecasting. Dereferencing operator as part of the pointer can be used for easy access or manipulation of the stored data in the memory location for the pointer pointing the data type and it will have a direct impact on the value of the data type.
Examples of Void Pointer in C
Following are the examples as given below:
Example #1
This program illustrated the void Pointer in C as it is not associated with any data type at the time of declaration as shown in the given output.
Code:
int r = 11;
char m = 'k';
void *p = &r;
p = &m;
Output:
Example #2
This program is used to illustrate the dereferencing of the void pointer of C where the input is given to variable with the inception operator which is shown with the following example.
Code:
#include<stdio.h>
int main()
{
int o = 18;
void *ptr = &o;
printf("%d", *(int *)ptr);
return 0;
}
Output:
Example #3
This program illustrates the void pointer for representing the size of integer value define with the arithmetic operator which means it manages to support the arithmetic operator in association with a void pointer. Although it needs to follow some of the standards of the GNU then only the compilation allowed will provide the necessary output as shown otherwise it will give a compilation error.
Code:
#include<stdio.h>
int main()
{
int q[3] = {5,9,7};
void *ptr = &q;
ptr = ptr + sizeof(int);
printf("%d", *(int *)ptr);
return 0;
}
Output:
Example #4
This program is used to illustrate the basic concept of dereferencing with the void pointer in C and its output is as follows.
Code:
#include<stdio.h>
void main()
{
int r=17;
float q=13.8;
char i='c';
void *p;
p=&r;
printf("%d",*((int*)p));
p=&q;
printf("n%f",*((float*)p));
p=&i;
printf("n%c",*((char*)p));
}
Output:
Conclusion
void pointer in C is used to mitigate the problem of pointers pointing to each other with a different set of values and data types. There is also a reduction in explicit typecasting. Also, it supports the generic pointer type which makes it as a generic-purpose compiler.
Recommended Articles
This is a guide to Void Pointer in C. Here we also discuss the definition and how does void pointer work in c? along with different examples and code implementation. You may also have a look at the following articles to learn more –