Search code examples
cmallocdynamic-allocation

Malloc returning Void in C


Guys I'm programing in C, trying to do a dynamic allocation of a type char like this :

char **word1 = malloc(sizeof(char *)* 1);
char **word2 = malloc(sizeof(char *) * 1);

But it is resulting an erro like that: invalid conversion from 'void*' to 'char**' [-fpermissive]

Thanks every one who help me.


Solution

  • In c compiler [gcc], this error would not show up.

    In c++ compiler, [g++], this error is likely to happen.

    To get rid of this,either

    1. Use a c compiler to compile the above code.
    2. Use a c++ compiler and add a char ** cast to malloc() return value.

    Note: IMO, go for the 1st point. It's not a good practice neither to use malloc() family in c++, nor casting the return of malloc().