3

We all know that one shouldn't cast the void * return of malloc/calloc, but what of similar functions that return void **? I have the following function:

void **calloc_2d(const size_t ptr_count, const size_t size_buffers, const size_t size_elem)
{
    void **array = calloc(ptr_count, sizeof(void *));
    for (size_t i=0; i < ptr_count; i++)
        array[i] = calloc(size_buffers, size_elem);
    return array;
}

And I may call it this way:

char **string_array = calloc_2d(5, 32, sizeof(char));

But unlike with regular calloc here Visual Studio tells me

Warning C4133 '=': incompatible types - from 'void **' to 'char **'

It sounds like I'm expected to cast it, why? And is there any way to avoid casting for every call to this function?

GSerg
  • 76,472
  • 17
  • 159
  • 346
Michel Rouzic
  • 1,013
  • 1
  • 9
  • 22

0 Answers0