铸造空**回报

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;
}

我可以这样称呼它:

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

但是与常规的calloc不同,Visual Studio告诉我

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

听起来像我要投的票,为什么?还有什么方法可以避免每次调用此函数时都强制转换?

评论