void *pointer name;
Declaration of the void pointer is given below:
void *ptr;
In the provided declaration, the void represents the pointer's data type, while 'ptr' serves as the pointer's identifier.
Let us consider some examples:
Initialize an integer variable named i with the value of 9.
int *p; // integer pointer declaration.
Declare a pointer named fp for floating-point data types.
void *ptr; // void pointer declaration.
p=fp; // incorrect.
fp=&i; // incorrect
ptr=p; // correct
ptr=fp; // correct
ptr=&i; // correct
Size of the void pointer in C
The size of a void pointer in C is identical to the size of a pointer of character type. In C, a pointer to void is perceived as having the same representation as a pointer of character type. The size of the pointer may differ based on the specific platform being utilized.
Let's look at the below example:
#include <stdio.h>
int main()
{
void *ptr = NULL; //void pointer
int *p = NULL;// integer pointer
char *cp = NULL;//character pointer
float *fp = NULL;//floaLogic Practiceer
//size of void pointer
printf("size of void pointer = %d\n\n",sizeof(ptr));
//size of integer pointer
printf("size of integer pointer = %d\n\n",sizeof(p));
//size of character pointer
printf("size of character pointer = %d\n\n",sizeof(cp));
//size of floaLogic Practiceer
printf("size of floaLogic Practiceer = %d\n\n",sizeof(fp));
return 0;
}
Output
[Program Output]
Advantages of void pointer
Here are the benefits of a null pointer:
- The void pointer is returned by the malloc and calloc functions, allowing for the allocation of 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 which is pointed by x pointer : %d",*x);
return 0;
}
Output
[Program Output]
- Utilizing the void pointer in C is also applicable for creating generic functions in the C programming language.
Some crucial Logic Practices associated with void pointers include:
- Performing indirection on a void pointer in the C programming language.
The void pointer in C cannot be dereferenced directly. Let's see the below example.
#include <stdio.h>
int main()
{
int a=90;
void *ptr;
ptr=&a;
printf("Value which is pointed by ptr pointer : %d",*ptr);
return 0;
}
In the provided code, *ptr represents a void pointer that points to the integer variable 'a'. Since we understand that dereferencing a void pointer is not allowed, the code mentioned will result in a compile-time error when attempting to print the value of the variable pointed to by the pointer 'ptr' directly.
Output
[Program Output]
Now, we revise the preceding code to eliminate the mistake.
#include <stdio.h>
int main()
{
int a=90;
void *ptr;
ptr=&a;
printf("Value which is pointed by ptr pointer : %d",*(int*)ptr);
return 0;
}
In the provided code snippet, we perform a type conversion of the void pointer to the integer pointer using the following statement:
(int*)ptr;
Next, we display the value of the variable indicated by the void pointer 'ptr' using the following statement:
(int)ptr;
Output
Performing mathematical operations on void pointers is not allowed in C programming.
Arithmetic operations cannot be directly executed on void pointers in C. To enable arithmetic operations on void pointers, appropriate typecasting is required.
Let's see the below example:
#include<stdio.h>
int 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.
}}
The code snippet above demonstrates a compile-time error indicating an "invalid use of void expression." This error occurs when attempting arithmetic operations directly on a void pointer, such as ptr=ptr+1, which is not allowed.
Let's rewrite the above code to remove the error.
#include<stdio.h>
int 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,",*((float*)ptr+i));
}}
The preceding code executes without errors after correctly casting the void pointer as (float)ptr and subsequently performing the arithmetic operation as ((float*)ptr+i).
Output
[Program Output]
Why we use void pointers?
We opt for void pointers due to their versatility. They have the ability to hold objects of any data type, and we can access these objects of any type by employing the indirection operator along with appropriate type conversions.
Let's understand through an example.
#include<stdio.h>
int main()
{
int a=56; // initialization of a integer variable 'a'.
float b=4.5; // initialization of a float variable 'b'.
char c='k'; // initialization of a char variable 'c'.
void *ptr; // declaration of void pointer.
// 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));
return 0;
}
Output