我希望将元素的类型作为参数提供给指向未知类型元素的指针数组的初始化
就像这样
void* init(type t)
void* array = malloc(sizeof_type(t)*10));
return array;
}然后再调用例如
init(typeof(int))但是我不知道typeof的返回类型是什么。
我猜sizeof_type think可以通过以下方式实现
malloc((type) 0);提前感谢
PS:如果这是一个向量实现,如果有人能告诉我一些灵活的代码,我也会非常感激的
发布于 2010-07-16 22:32:13
您可以使用宏来实现此目的:
#define INIT(t) ((t*) init_1(sizeof(t)))
void *init_1(size_t sz) {
void *array = malloc(sz * 10);
return array;
}然后使用like:
int *array = INIT(int);https://stackoverflow.com/questions/3265676
复制相似问题