首页 > 解决方案 > 铸造一个 void ** return

问题描述

我们都知道不应该将/的void *返回值强制转换,但是返回的类似函数呢?我有以下功能:malloccallocvoid **

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 告诉我

警告 C4133 '=':不兼容的类型 - 从 'void **' 到 'char **'

听起来我应该投它,为什么?有没有什么办法可以避免每次调用这个函数时强制转换?

标签: cvisual-studiopointerscasting

解决方案


推荐阅读