Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How does ‘void*’ differ in C and C++?

  • Difficulty Level : Easy
  • Last Updated : 28 Nov, 2021

C allows a void* pointer to be assigned to any pointer type without a cast, whereas in C++, it does not. We have to explicitly typecast the void* pointer in C++

For example, the following is valid in C but not C++: 

void* ptr;
int *i = ptr; // Implicit conversion from void* to int*

Similarly,

int *j = malloc(sizeof(int) * 5);  // Implicit conversion from void* to int* 

In order to make the above code compile in C++ as well, we have to use explicit casting, as shown below,

void* ptr;
int *i = (int *) ptr;
int *j = (int *) malloc(sizeof(int) * 5);

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!