Home >Backend Development >C++ >What is the difference between 'void *' in C and C++?
In the following example, when we write some code, we can execute these lines The code is in C.
void *p; int *int_ptr = p;
This works fine in C. Now, if we use malloc() to allocate some memory space, we It's possible to use explicit type conversion, but if we don't do that, no problem. The malloc() The function returns a null pointer.
int *int_ptr = malloc(sizeof(int) * 10);
The void pointer returned here is implicitly converted to an integer type pointer.
Now if we want to run the same program in C and C, we should explicitly type cast pointer.
void *p; int *int_ptr = (int *) p; int *arr_ptr = (int *) malloc(sizeof(int) * 10);
The above is the detailed content of What is the difference between 'void *' in C and C++?. For more information, please follow other related articles on the PHP Chinese website!