How to Fix: Invalid Conversion From 'void*' to 'char*' When Using malloc

I’m currently learning C and C++. At the beginning, I wasn’t sure if C++ was merely an extension to C or if they were, despite their shared history, two different languages. A recent issue with the malloc function gave me a deeper insight into this question.

As I was working on a C++ project, I wanted to use malloc in the following way:

1char *buffer = malloc(21);

However, the compiler threw the following error:

Through my research, I discovered that my code, while valid C, is invalid C++. In C++, you are required to typecast the pointer returned by malloc. Thus, my code should really read as follows:

1char *buffer = (char*) malloc(21);

For more information about the differences between C and C++, I suggest you read the following articles: C++ is not a superset of C from software engineer Hannah McLaughlin and Is C a subset of C++? from the creator of C++, Bjarne Stroustrup.