Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does “void *” differ in C and C++?
Both C and C++ support void pointers, but their behavior differs significantly. In C, a void pointer can be directly assigned to any other pointer type without explicit typecasting. However, in C++, assigning a void pointer to any other pointer type requires an explicit typecast.
Syntax
void *pointer_name;
Void Pointer in C
A void pointer (also called a generic pointer) in C is a special type of pointer that can point to any data type, but doesn't have any type by itself. It can hold the address of any variable (int, float, char, etc.).
In C, void pointers provide implicit conversion −
int *int_ptr = p; // Implicit conversion allowed int *arr = malloc(sizeof(int) * 10); // No cast needed from malloc
The malloc() function returns a void pointer that is implicitly converted to the target pointer type.
Example
The following program demonstrates how a void* pointer can point to any type and how memory allocation works in C −
#include <stdio.h>
#include <stdlib.h>
int main() {
void *p;
int x = 10;
p = &x;
// Implicit cast from void* to int* (valid in C)
int *int_ptr = p;
printf("Value: %d\n", *int_ptr);
// malloc returns void*, allowed without cast in C
int *arr = malloc(5 * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
arr[0] = 100;
printf("Array First Value: %d\n", arr[0]);
free(arr);
return 0;
}
Value: 10 Array First Value: 100
Void Pointer in C++
In C++, a void pointer can store the address of any data type, but C++ enforces strict type checking. All conversions from void* to specific pointer types must be explicit.
C++ requires explicit casting −
int *int_ptr = (int *)p; // Explicit cast required int *arr_ptr = (int *)malloc(sizeof(int) * 10); // Cast needed
Example
This program demonstrates void* usage in C++ with required explicit casting −
#include <stdio.h>
#include <stdlib.h>
int main() {
void *p;
int x = 10;
p = &x;
// Explicit cast required in C++
int *int_ptr = (int *)p;
printf("Value: %d\n", *int_ptr);
// Explicit cast required for malloc in C++
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
arr[0] = 100;
printf("Array First Value: %d\n", arr[0]);
free(arr);
return 0;
}
Value: 10 Array First Value: 100
C vs C++ Void Pointers Comparison
| Feature | C | C++ |
|---|---|---|
| Implicit Conversion | Allowed | Not Allowed |
| Explicit Casting | Optional | Required |
| malloc() Assignment | Direct assignment | Must cast return value |
| Type Safety | Flexible | Strict |
| Pointer Arithmetic | Compiler-dependent | Not allowed without cast |
Conclusion
The main difference is that C allows implicit conversion from void* to other pointer types, while C++ requires explicit casting for type safety. This makes C++ more strict but safer in terms of type checking.
