开发者

How to cast void * to float (*)[3] in c++?

开发者 https://www.devze.com 2023-04-10 20:12 出处:网络
This is the code snippet. typedef struct Lib3dsMesh { //.. float (*vertices)[3]; //.. } void* lib3ds_util_realloc_array(void *ptr, in开发者_开发知识库t old_size, int new_size, intelement_size)

This is the code snippet.

typedef struct Lib3dsMesh {
 //..
 float (*vertices)[3]; 
 //..
}


void* lib3ds_util_realloc_array(void *ptr, in开发者_开发知识库t old_size, int new_size, int  element_size) {
    // Do something here.
    return ptr;
}


mesh->vertices = lib3ds_util_realloc_array(mesh->vertices, mesh->nvertices, nvertices, 3 * sizeof(float));

When I compile this code in visual c++ it returns error "Cannot convert from void* to float(*)[3]".

I would like to know how to cast void * to float (*vertices)[3];


vertices is a pointer to a 3-element array of float. To do a cast from one pointer type to another, you generally use static_cast:

void* result = lib3ds_util_realloc_array(
    mesh->vertices, mesh->nvertices, nvertices, 3 * sizeof(float));
mesh-vertices = static_cast<float (*)[3]>(result);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号