Void Pointer in C Language
A void pointer is a special type of pointer that can store the address of any data type. Since it is not associated with a specific data type, it is called a generic pointer. This flexibility makes void pointers useful in situations where the data type is not known in advance.
Key Features of a Void Pointer
- A void pointer is also known as a generic pointer.
- It can store the address of variables belonging to any data type.
- A void pointer cannot be dereferenced directly. It must first be typecast to the appropriate pointer type.
- In standard C, pointer arithmetic is not allowed on a void pointer because the compiler does not know the size of the referenced data type.
- Some compilers, such as GCC, allow pointer arithmetic on void pointers by treating
voidas having a size of 1 byte. However, this is a compiler-specific extension and is not part of the C standard. - Before performing dereferencing or pointer arithmetic, a void pointer should always be converted to the appropriate pointer type.
Syntax
void *pointerName;Example
void *ptr;
int num;
ptr = #Here, ptr is a void pointer. It stores the address of num, but it is not associated with any specific data type.
Since a void pointer has no type information, it cannot directly access or modify the value stored at the memory location. Therefore, it must first be converted (typecast) to the appropriate pointer type.
*(int *)ptr = 33; // Correct
*ptr = 33; // Error(int *)ptr converts the void pointer into an integer pointer. After typecasting, the dereference operator (*) accesses the value stored at that memory location.Program 1: Using a Void Pointer with an Integer Variable
The following program demonstrates how a void pointer can store the address of an integer variable. Since a void pointer has no associated data type, it must be typecast to an int * before accessing or modifying the value.
void main()
{
void *ptr;
int num;
ptr = #
*(int*)ptr = 23;
printf("Value : %d ", num);
printf("Value : %d", *(int*)ptr);
}Output
Value : 23
Value : 23- The void pointer
ptrstores the address of the integer variablenum. - The expression
(int *)ptrconverts the void pointer into an integer pointer. - The statement
*(int *)ptr = 23;stores the value 23 in the variablenum. - Both
numand*(int *)ptrrefer to the same memory location, so both print the value 23.
Program 2: Using a Void Pointer with a Float Variable
The following example shows that a void pointer can also store the address of a floating-point variable. Before accessing the value, the pointer must be converted to a float *.
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- The void pointer stores the address of the floating-point variable
num. - The typecast
(float *)ptrconverts the void pointer into a float pointer. - The statement
*(float *)ptr = 65.87;stores the value 65.87 in the variable. - Both print statements display the same value because they access the same memory location.
Key Observation
These two examples demonstrate that a single void pointer can work with different data types. However, before accessing the stored value, the pointer must always be typecast to the appropriate pointer type. Without proper typecasting, dereferencing a void pointer results in a compilation error.
Why Do We Use Void Pointers?
A void pointer provides flexibility because it is not restricted to a particular data type. It can be reused with different types of variables, making programs more generic and reusable.
Some of the major uses of void pointers are listed below.
- They can store the address of variables belonging to any data type.
- A single pointer variable can be reused for multiple data types, reducing the need for separate pointer variables.
- Dynamic Memory Allocation (DMA) functions such as
malloc()andcalloc()return avoid *pointer. - They are widely used for creating generic functions that can process different types of data.
- They improve code reusability by allowing the same function to work with multiple data types.
Program 3: Generic Function Using a Void Pointer
One of the biggest advantages of a void pointer is that it allows us to create generic functions. A generic function can work with multiple data types instead of being limited to a single type.
In the following program, the function printValue() accepts a void * pointer along with a character that specifies the data type. Based on the supplied type, the function converts the void pointer to the appropriate pointer type and prints the corresponding value.
// GENERIC FUNCTION USING VOID 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 : PProgram Explanation
- The function
printValue()receives a void pointer and a character representing the data type. - The character parameter (
'i','f', or'c') indicates whether the pointer refers to an integer, float, or character. - The
switchstatement checks the data type and converts the void pointer into the corresponding pointer type using typecasting. - After typecasting, the dereference operator (
*) accesses the actual value stored at that memory location. - The same function can therefore print values of different data types without creating separate functions for each type.
The function is called generic because it is not restricted to a single data type. By using a void * pointer together with typecasting, the same function can process integers, floating-point numbers, characters, and many other data types.
Type Casting Rules for Void Pointers in C and C++
The rules for assigning and converting void pointers are slightly different in C and C++. Understanding these differences is important for both competitive examinations and interviews.
Type Casting Rules for Void Pointers in C and C++
One important difference between C and C++ is the way they handle void pointer type conversions. Understanding this difference is essential because it is frequently asked in university examinations, competitive exams, and technical interviews.
Suppose T* represents any typed pointer such as int*, char*, float*, or double*. The following table explains whether typecasting is required while converting between a typed pointer and a void pointer.
| Conversion | C Language | C++ Language | Description |
|---|---|---|---|
| T* ? void* (Assigning a typed pointer to a void pointer) | Allowed No explicit typecasting is required. | Allowed No explicit typecasting is required. | A pointer of any data type can be assigned to a void pointer directly. |
| void* ? T* (Assigning a void pointer to a typed pointer) | Allowed Implicit conversion is permitted. | Not Allowed Explicit typecasting is mandatory. | In C++, the compiler requires the programmer to explicitly convert a void pointer to the appropriate pointer type. |
- In C, converting
void*to another pointer type does not require explicit typecasting. - In C++, converting
void*to another pointer type always requires explicit typecasting. - In both C and C++, assigning a typed pointer to a
void*pointer is allowed without any typecasting.
Program 4: Void Pointer Conversion in C
The following program demonstrates how pointer conversion works in the C programming language. Since C allows the implicit conversion of a void* pointer to another pointer type, no explicit typecasting is required while assigning the void pointer to an integer pointer.
// 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 : 33Program Explanation
ptr1stores the address of the integer variablenum.ptr3, which is a void pointer, stores the same address without requiring any typecasting.- In the C programming language, assigning a
void*pointer to another typed pointer (ptr2) is allowed implicitly. - All four print statements access the same memory location and therefore display the value 33.
void* to another pointer type is allowed in C, but the same statement will generate a compilation error in C++.Program 5: Void Pointer Conversion in C++
Unlike the C programming language, C++ does not allow the implicit conversion of a void* pointer to another typed pointer. Therefore, an explicit typecast is required before assigning a void pointer to a typed pointer.
The following program demonstrates this behavior in C++.
// Myfile.cpp
int main()
{
int num = 33;
int *ptr1, *ptr2;
void *ptr3;
ptr1 = #
ptr3 = ptr1; // T* ? void* (Implicit)
// ptr2 = ptr3; // Error (Implicit conversion is not allowed)
ptr2 = (int*)ptr3; // Explicit typecasting
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 : 33Program Explanation
- The pointer
ptr1stores the address of the integer variablenum. - The statement
ptr3 = ptr1;is valid because both C and C++ allow assigning a typed pointer to a void pointer without explicit typecasting. - The statement
ptr2 = ptr3;is commented out because it generates a compilation error in C++. - The correct approach is
ptr2 = (int*)ptr3;, where the void pointer is explicitly converted into an integer pointer. - All four print statements display the same value because every pointer refers to the same memory location.
void* pointer to another pointer type.Key Differences Between C and C++
| Feature | C | C++ |
|---|---|---|
Typed Pointer ? void* | Allowed without typecasting. | Allowed without typecasting. |
void* ? Typed Pointer | Allowed without explicit typecasting. | Explicit typecasting is required. |
Direct Dereferencing of void* | Not Allowed. | Not Allowed. |
Pointer Arithmetic on void* | Not allowed in standard C. | Not allowed. |
Quick Revision
- A void pointer can store the address of any data type.
- A void pointer is also known as a generic pointer.
- A void pointer cannot be dereferenced directly.
- Always convert a void pointer to the appropriate pointer type before dereferencing.
- Pointer arithmetic on a void pointer is not permitted in standard C because the size of
voidis unknown. - Functions such as
malloc()andcalloc()return avoid*pointer. - In C, converting
void*to another pointer type is allowed implicitly. - In C++, converting
void*to another pointer type requires explicit typecasting.
Conclusion
A void pointer is one of the most flexible pointer types in C because it can store the address of variables belonging to any data type. This flexibility makes it extremely useful for generic programming and dynamic memory allocation.
However, since a void pointer has no associated data type, it cannot directly access the value stored at a memory location. It must always be converted to the appropriate pointer type before dereferencing or performing pointer operations.
Understanding void pointers is essential for mastering topics such as Dynamic Memory Allocation (DMA), generic programming, and advanced pointer concepts. It is also a frequently asked topic in university examinations, competitive exams, and technical interviews.
Practice using void pointers with different data types and write small programs to strengthen your understanding of typecasting and generic programming concepts. The more you practice, the more confident you will become in working with pointers.
