Void Pointer In C [Explained With Examples]
As we know, a pointer only stores the address of a variable with the same data type as it is. For example, if we declare an int pointer, then this int pointer cannot point to float variable or any other type of variable, with the help of this pointer we can only point to an integer type variable.
In such a situation, to overcome this problem, the concept of void pointer came in C language.
So now without wasting any time let’s understand What is Void Pointer In C
What is Void Pointer In C
Void pointer is a generic pointer that has no relation to any data type. It can store the address of a variable of any type and can be easily type-casted to any data type.
The declaration of void pointer is similar to a normal pointer, but the void keyword is used while declaring a void pointer.
Syntax -:
void *pointer_name;
Example -:
void *ptr;
Here void is a keyword using which we are declaring a void pointer and ptr is a void pointer.
What is the size of a void pointer in C?
The size of the void pointer is the same as the size of character type pointer. Its representation is also like a character pointer.
Note -: The size of pointers depends on which platform you are using, it can be 2bytes, 4bytes, or 8bytes…etc.
Example -:
#include <stdio.h>
void main()
{
/*Declaration of void pointer, integer pointer character pointer and float pointer
*/
void *ptr = NULL;
int *p = NULL;
char *cp = NULL;
float *fp = NULL;
//size of void pointer
printf("size of void pointer = %d\n",sizeof(ptr));
//size of integer pointer
printf("size of integer pointer = %d\n",sizeof(p));
//size of character pointer
printf("size of character pointer = %d\n",sizeof(cp));
//size of float pointer
printf("size of float pointer = %d\n",sizeof(fp));
}
Output -:
On a 32-bit system machine
size of void pointer = 4
size of integer pointer = 4
size of character pointer = 4
size of float pointer = 4
Why we use void pointers
We use void pointers because of their reusability. void pointers can store the address of any type of object and we can retrieve objects of any type with proper typecasting using the indirection operator.
Example -:
#include<stdio.h>
void main()
{
//Declaration & initialization of different pointers
int a=6;
float b=5.2;
char c='J';
void *ptr;
// assigning the address of variable 'a'
ptr=&a;
printf("value of 'a' is : %d",*((int*)ptr));
// assigning the address of variable 'b'
ptr=&b;
printf("\nvalue of 'b' is : %f",*((float*)ptr));
// assigning the address of variable 'c'
ptr=&c;
printf("\nvalue of 'c' is : %c",*((char*)ptr));
}
Output -:
value of 'a' is : 6
value of 'b' is : 5.200000
value of 'c' is : J
Explanation -:
First of all, look at the above program carefully! In this program, we have created a void pointer named ptr which points in turn to the address of variables (a, b, c) of different types (int, char, float) and returns their values using the indirection operator and proper typecasting.
Advantages of Void Pointers
- The malloc() and calloc() functions return void pointer, so these functions are used to allocate memory of any data type.
- The void pointer is used in C language to implement the concept of generic functions.
- We can also create a generic linked list by using void pointer.
Important points about void pointer
- Pointer arithmetic operations cannot be performed with void pointer.
- It cannot be used as dereferenced.
Let us understand these two things with an example.
1) Dereferencing a void pointer in C
Like other pointers, we cannot dereference void pointers because the compiler does not have any information about the point object. If we try to compile the below code then we will get a compiler error.
#include <stdio.h>
void main()
{
int a=90;
void *ptr;
ptr=&a;
printf("Value which is pointed by ptr pointer : %d",*ptr);
}
Output -:
warning: dereferencing 'void *' pointer
2) Arithmetic operation on void pointers
We cannot apply arithmetic operations directly on void pointers. We need to do proper typecasting so that we can perform arithmetic operations on void pointers.
Example -:
#include<stdio.h>
void main()
{
float a[4]={6.1,2.3,7.8,9.0};
void *ptr;
ptr=a;
for(int i=0;i<4;i++)
{
printf("%f,",*ptr);
ptr=ptr+1; // Incorrect.
}
}
Running the above code will give compile-time error that “invalid use of void expression” because we cannot directly apply arithmetic operations ( ptr=ptr+1) on void pointer.
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is Void Pointer In C
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Void Pointer In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.