Void Pointer in C Language:
The void pointer can hold or store address of any data type. A void pointer is a special type of pointer that is not associated with any data type.
- The void pointer is also known as generic pointer.
- A void pointer cannot be directly dereferenced (must be cast to another pointer type before dereferencing).
- Because the size of a void type is unknown, pointer arithmetic operations are not allowed with a void pointer in standard C (Compile-Time Error: “Size of the type is unknown or zero”). However, in GCC, pointer arithmetic works because it treats void as having a size of 1 byte (non-standard behavior).
- The void pointer must be cast to appropriate data type before dereferencing and performing arithmetic operations.
Syntax:
void *pointerName;Example:
void *ptr;int num;
ptr = #
Here, ptr is a pointer to void, meaning it points to a memory location but has no specific type.
If we want to assign a value to the variable num through the pointer ptr, or access num using ptr, we must perform proper type casting, because ptr is a void* and has no type information.
*(int *)ptr = 33; // assign 33 to num via ptr. *ptr = 33; is error.
Here, the leftmost * is the indirection (dereference) operator — it accesses the value stored at the address and the (int *) part typecasts the void* pointer into an int* pointer before dereferencing.
Program : 1
void main()
{
void *ptr;
int num;
ptr = #
*(int*)ptr = 23;
printf("Value : %d ",num);
printf("Value : %d",*(int*)ptr);
}
Output:
Value : 23
Value : 23
Program : 2
void main()
{
void *ptr;
float num;
ptr = #
*(float*)ptr = 65.87;
printf("Value : % .2f ",num);
printf("Value : % .2f",*(float*)ptr);
}
Output:
Value : 65.87
Value : 65.87
This way, by casting void* to the correct type pointer, we can both assign and access values stored in different variable types through the same pointer.
Why Use Void Pointers?
- Void pointers provide flexibility when the exact data type is not known in advance.
- One pointer can be reused for different types without declaring multiple pointer variables.
- The Dynamic Memory Allocation (DMA) functions like malloc() and calloc() return void pointers.
- For generic data handling, writing functions that can accept pointers to any data type.
Program : 3
//GENERIC FUNCTION USING POINTER.
void printValue(void *,char);
void main()
{
int a = 33;
float b = 69.14;
char h = 'P';
printValue(&a,'i');
printValue(&b,'f');
printValue(&h,'c');
}
void printValue(void *ptr,char type)
{
switch(type)
{
case 'c':
printf("Character Value : %c ", *(char*)ptr);
break;
case 'i':
printf("Integer Value : %d ", *(int*)ptr);
break;
case 'f':
printf("Float Value : %.2f ", *(float*)ptr);
break;
default:
printf("Sorry | Invalid Type.");
}
}
Output:
Integer Value : 33
Float Value : 69.14
Character Value : P
Must Know:
Casting is required when assigning a void pointer to another type in C++, but not mandatory in C. Suppose T* is any typed pointer like char, int, float etc. and void* is void pointer, then:C Language | Allowed? | C++ Language | Allowed? |
---|---|---|---|
T* ? void* (Assigning of any typed pointer to void pointer) |
Yes Implicit, no casting needed. |
T* ? void* (Assigning of any typed pointer to void pointer) |
Yes Implicit, no casting needed. |
void* ? T* (Assigning of void pointer to any typed pointer) |
Yes Implicit, no casting needed. |
void* ? T* (Assigning of void pointer to any typed pointer) |
No Explicit, casting needed. |
Program : 4
// Myfile.c
void main()
{
int num=33;
int *ptr1,*ptr2;
void *ptr3;
ptr1=#
ptr3=ptr1; //T* ? void* (implicit)
ptr2=ptr3; //void* ? T* (implicit)
printf("Value : %d ",num);
printf("Value : %d ",*ptr1);
printf("Value : %d ",*ptr2);
printf("Value : %d ",*(int*)ptr3);
}
Output:
Value : 33
Value : 33
Value : 33
Value : 33
Program: 5
//Myfile.cpp
int main()
{
int num=33;
int *ptr1,*ptr2;
void *ptr3;
ptr1=#
ptr3=ptr1; //T* ? void* (implicit)
//ptr2=ptr3; Error(Not implicit)
ptr2=(int*)ptr3; //Explicit cast required
printf("Value : %d ",num);
printf("Value : %d ",*ptr1);
printf("Value : %d ",*ptr2);
printf("Value : %d ",*(int*)ptr3);
return 0;
}
Output:
Value : 33
Value : 33
Value : 33
Value : 33