void* in C vs C++
How does ‘void*’ differ in C and C++ ?
On this page we will discuss about difference in void* in C vs C++ . void* is a type of pointer that points to an area of memory holding data of an unknown type. It is often used as a generic type for passing pointers around in a code, since it can hold the address of any type of object.
Difference in void* in C vs C++
The void*
type behaves similarly in both C and C++, as it is used to represent a pointer to an area of memory that holds data of an unknown type. However, there are a few key differences between the way void*
is used in these two languages:
- In C++,
void*
can be used as the type of a function parameter or return value, while in C it cannot. In C, you must use a specific pointer type instead. - In C++,
void*
can be explicitly converted to and from any other pointer type using the static_cast operator. In C, you must use a type cast to perform this conversion. - In C++,
void*
cannot be dereferenced directly. You must first cast it to a pointer of a specific type before you can use it to access the data it points to. In C, you can dereference avoid*
pointer using a type cast.
void *p; int *int_ptr = (int *) p; int *arr_ptr = (int *) malloc(sizeof(int) * 10);
This code will execute in both c and c++ .
Example of void* in C
#include <stdio.h> // Function that takes a void* argument and prints the value it points to void print_value(void* p) { // Must cast void* to a pointer of a specific type before dereferencing int* q = (int*)p; printf("%d\n", *q); } int main() { int x = 10; // Assign address of x to a void* pointer void* p = &x; // Pass void* pointer to function as argument print_value(p); return 0; }
Output:
10
In the C example, the print_value function does the same thing, but uses a type cast instead of the static_cast operator to convert the void*
to an int*
.
Example of void* in C++
#include <iostream> using namespace std; // Function that takes a void* argument and prints the value it points to void print_value(void* p) { // Must cast void* to a pointer of a specific type before dereferencing int* q = static_cast(p); cout << *q << endl; } int main() { int x = 10; // Assign address of x to a void* pointer void* p = &x; // Pass void* pointer to function as argument print_value(p); return 0; }
Output:
10
In the C++ example, the print_value function takes a void*
argument and prints the value it points to by first casting the pointer to an int*
and then dereferencing it.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment