How does “void *” differ in C and C++?
C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; this idiom appears often in C code using malloc memory allocation. For example, the following is valid in C but not C++:
void* ptr; int *i = ptr; /* Implicit conversion from void* to int* */ |
chevron_right
filter_none
or similarly:
int *j = malloc(sizeof(int) * 5); /* Implicit conversion from void* to int* */ |
chevron_right
filter_none
In order to make the code compile in both C and C++, one must use an explicit cast:
void* ptr; int *i = (int *) ptr; int *j = (int *) malloc(sizeof(int) * 5); |
chevron_right
filter_none
Source:
http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
Recommended Posts:
- Implementing upper_bound() and lower_bound() for Ordered Set in C++
- How to flatten a Vector of Vectors or 2D Vector in C++
- <complex.h> header file in C with Examples
- Remove odd frequency characters from the string
- Different ways to use Const with Reference to a Pointer in C++
- std::to_address in C++ with Examples
- Program to create Custom Vector Class in C++
- std::is_trivially_copy_constructible in C/C++
- Difference between Python and C++
- tgamma() method in C/C++ with Examples
- boost::type_traits::is_array Template in C++
- boost is_pointer template in C++
- Modulo Operator (%) in C/C++ with Examples
- fpclassify() method in C/C++ with Examples

