我正在编写一个C程序,它根据环境变量对数组应用不同类型的量化.
问题是,我需要使用相同的函数void foo(void* ptr, quant_t type, ...)
在PTR上执行操作,但我需要事先将其强制转换为正确的类型.
(quant_t是枚举类型对象)
我试着go 做
void foo(void* ptr, quant_t type, ...){
switch(type){
case UNIF:{
struct unif* my_ptr = (struct unif*) ptr;
break;
}
case KMEANS:{
struct kmeans* my_ptr = (struct kmeans*) ptr;
break;
}...}
my_ptr->a = bar(...);
my_ptr->b = baz(...);
}
但它不起作用,因为my_ptr的声明在切换用例的作用域内.
So 我试着go 做 something like this:
void foo(void* ptr, quant_t type, ...){
void* my_ptr = NULL;
switch(type){
case UNIF:{
my_ptr = (struct unif*) ptr;
break;
}
case KMEANS:{
my_ptr = (struct kmeans*) ptr;
break;
}...}
my_ptr->a = bar(...);
my_ptr->b = baz(...);
}
但还是不行.