Dereferencing a Void Pointer
A void pointer cannot be dereferenced simply by using the indirection operator. Here is an example.
#include <stdio.h>
int main()
{
int i=15;
void *ptr;
ptr=&i;
printf("%d",*ptr);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Compilation of the above program will give a compile-time error because we already know that the void pointer cannot be dereferenced directly.
Here, we removed the compile-time error by typecasting the void pointer to the integer pointer.
#include <stdio.h>
int main()
{
int i=15;
void *ptr;
ptr=&i;
printf("%d",*(int*)ptr);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
15
The above program compiles and runs fine.
Arithmetic Operation on Void Pointers
As we saw before, a void pointer cannot be dereferenced directly. Similarly, pointer arithmetic cannot be performed on void pointers without typecasting.
Example
#include<stdio.h>
int main()
{
float arr[3]={1.5,2.3,5.4};
void *ptr;
ptr=arr;
for(int i=0;i<3;i++)
{
printf("%.1f\n", *(float *)ptr);
ptr=(float*)ptr+1; // cannot write ptr=ptr+1.
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
1.5
2.3
5.4
Here the above program does not give any error as we applied the proper typecasting to the void pointer, i.e., (float*)ptr. Then we applied the arithmetic operation, i.e., ptr=((float*)ptr+1).
You can also read about the dynamic arrays in c, And Tribonacci Series
Advantages of Void Pointer
-
The malloc() and calloc() function return void * or generic pointer, so we can use these functions to allocate memory of any data type.
Example
#include <stdio.h>
#include<malloc.h>
int main()
{
int i=15;
int *ptr=(int*)malloc(sizeof(int));
ptr=&i;
printf("Value pointed at by ptr: %d",*ptr);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Value pointed at by ptr: 15
-
The void pointer in C can be used to implement the generic functions in C.
Read about Bitwise Operators in C and Short int in C Programming
FAQs
-
What is the void pointer in C?
A void pointer in the C language is a pointer that does not have any associated data type.
-
How can we dereference the void pointer?
To dereference a void pointer, we must typecast it to a valid pointer type.
-
What is the correct syntax of a void pointer?
The correct syntax of a void pointer is void *pointer_name;.
-
What is the size of the void pointer?
The size of the void pointer depends on your platform.
On a 64-bit platform, you need 8 bytes to store a memory address.
On a 32 bit platform, you need 4 bytes to store a memory address
On a 16-bit platform, you need 2 bytes to store a memory address.
Key Takeaways
In this article, we have extensively discussed the void pointers topic and their implementation in the C programming language. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our article on pointers and visit our library of curated blogs by clicking here. Do upvote our blog to help other ninjas grow. Happy Coding!