C 使用不同数量和类型的参数调用函数的机制?
我有一组要调用的函数 e、 g: 我已经为调用上述任何函数定义了以下函数指针和参数:C 使用不同数量和类型的参数调用函数的机制?,c,void-pointers,type-conversion,C,Void Pointers,Type Conversion,我有一组要调用的函数 e、 g: 我已经为调用上述任何函数定义了以下函数指针和参数: typedef void * (*funcptr)(); typedef void * (*funcptr1)(void *); typedef void * (*funcptr2)(void *, void *); void * parameter1; void * parameter2; void * return_param; typedef struct funcid { int id;
typedef void * (*funcptr)();
typedef void * (*funcptr1)(void *);
typedef void * (*funcptr2)(void *, void *);
void * parameter1;
void * parameter2;
void * return_param;
typedef struct funcid {
int id;
funcptr f0;
funcptr1 f1;
funcptr2 f2;
void * param1;
void * param2;
void * ret_param;
} func_id;
printf("\nEnter Number of parameters of functions : ");
scanf("%d", ptr1);
printf("\nEnter whether the function has a return value (0/1) : ");
scanf("%d", &ret);
printf("\nWhich function to execute (function id): ");
scanf("%d", &var1);
if (*ptr1 > 0)
printf("\nEnter the Parameters : \n");
func_id current_func;
for (i=0; i<13; i++){
if (func_str[i].id == var1) {
current_func = func_str[i];
break;
}
}
switch(*ptr1) {
case 0: {
if (ret == 1) {
printf("\nEntering case 0\n");
current_func.ret_param = (*current_func.f0)();
}
else {
(*current_func.f0)();
}
break;
}
/* In actual case the parameters would be read from a memory location and the address of that can be stored in parameter1 */
case 1: {
printf("\nEnter Parameter 1: ");
scanf("%d", parameter1);
printf("\nParameter 1 = %d\n",*(int *)parameter1);
if (ret == 1) {
current_func.ret_param = (*current_func.f1)(current_func.param1);
}
else {
(*current_func.f1)(current_func.param1);
}
break;
}
/* In actual case the parameters would be written in a memory location and the address of that can be stored in parameter1 and parameter2 */
case 2: {
printf("\nEnter Parameter 1: ");
scanf("%d", parameter1);
printf("\nEnter Parameter 2: ");
scanf("%d", parameter2);
if (ret == 1) {
current_func.ret_param = (*current_func.f2)(current_func.param1, current_func.param2);
printf("\nReturned parameter : current_func.ret_param = %d", *(int *)current_func.ret_param);
}
else {
(*current_func.f2)(current_func.param1, current_func.param2);
}
break;
}
default:
printf("ERROR");
}
我已经定义了一个用于调用所有函数的自定义结构:
typedef void * (*funcptr)();
typedef void * (*funcptr1)(void *);
typedef void * (*funcptr2)(void *, void *);
void * parameter1;
void * parameter2;
void * return_param;
typedef struct funcid {
int id;
funcptr f0;
funcptr1 f1;
funcptr2 f2;
void * param1;
void * param2;
void * ret_param;
} func_id;
printf("\nEnter Number of parameters of functions : ");
scanf("%d", ptr1);
printf("\nEnter whether the function has a return value (0/1) : ");
scanf("%d", &ret);
printf("\nWhich function to execute (function id): ");
scanf("%d", &var1);
if (*ptr1 > 0)
printf("\nEnter the Parameters : \n");
func_id current_func;
for (i=0; i<13; i++){
if (func_str[i].id == var1) {
current_func = func_str[i];
break;
}
}
switch(*ptr1) {
case 0: {
if (ret == 1) {
printf("\nEntering case 0\n");
current_func.ret_param = (*current_func.f0)();
}
else {
(*current_func.f0)();
}
break;
}
/* In actual case the parameters would be read from a memory location and the address of that can be stored in parameter1 */
case 1: {
printf("\nEnter Parameter 1: ");
scanf("%d", parameter1);
printf("\nParameter 1 = %d\n",*(int *)parameter1);
if (ret == 1) {
current_func.ret_param = (*current_func.f1)(current_func.param1);
}
else {
(*current_func.f1)(current_func.param1);
}
break;
}
/* In actual case the parameters would be written in a memory location and the address of that can be stored in parameter1 and parameter2 */
case 2: {
printf("\nEnter Parameter 1: ");
scanf("%d", parameter1);
printf("\nEnter Parameter 2: ");
scanf("%d", parameter2);
if (ret == 1) {
current_func.ret_param = (*current_func.f2)(current_func.param1, current_func.param2);
printf("\nReturned parameter : current_func.ret_param = %d", *(int *)current_func.ret_param);
}
else {
(*current_func.f2)(current_func.param1, current_func.param2);
}
break;
}
default:
printf("ERROR");
}
我已经根据id初始化了结构,如下所示(在任何时候都只调用上述函数之一):
现在,在switch case语句中,我尝试调用以下任何函数:
typedef void * (*funcptr)();
typedef void * (*funcptr1)(void *);
typedef void * (*funcptr2)(void *, void *);
void * parameter1;
void * parameter2;
void * return_param;
typedef struct funcid {
int id;
funcptr f0;
funcptr1 f1;
funcptr2 f2;
void * param1;
void * param2;
void * ret_param;
} func_id;
printf("\nEnter Number of parameters of functions : ");
scanf("%d", ptr1);
printf("\nEnter whether the function has a return value (0/1) : ");
scanf("%d", &ret);
printf("\nWhich function to execute (function id): ");
scanf("%d", &var1);
if (*ptr1 > 0)
printf("\nEnter the Parameters : \n");
func_id current_func;
for (i=0; i<13; i++){
if (func_str[i].id == var1) {
current_func = func_str[i];
break;
}
}
switch(*ptr1) {
case 0: {
if (ret == 1) {
printf("\nEntering case 0\n");
current_func.ret_param = (*current_func.f0)();
}
else {
(*current_func.f0)();
}
break;
}
/* In actual case the parameters would be read from a memory location and the address of that can be stored in parameter1 */
case 1: {
printf("\nEnter Parameter 1: ");
scanf("%d", parameter1);
printf("\nParameter 1 = %d\n",*(int *)parameter1);
if (ret == 1) {
current_func.ret_param = (*current_func.f1)(current_func.param1);
}
else {
(*current_func.f1)(current_func.param1);
}
break;
}
/* In actual case the parameters would be written in a memory location and the address of that can be stored in parameter1 and parameter2 */
case 2: {
printf("\nEnter Parameter 1: ");
scanf("%d", parameter1);
printf("\nEnter Parameter 2: ");
scanf("%d", parameter2);
if (ret == 1) {
current_func.ret_param = (*current_func.f2)(current_func.param1, current_func.param2);
printf("\nReturned parameter : current_func.ret_param = %d", *(int *)current_func.ret_param);
}
else {
(*current_func.f2)(current_func.param1, current_func.param2);
}
break;
}
default:
printf("ERROR");
}
printf(“\n输入函数的参数数:”);
扫描频率(“%d”,ptr1);
printf(“\n输入函数是否有返回值(0/1):”;
scanf(“%d”、&ret);
printf(“\n要执行哪个函数(函数id):”;
scanf(“%d”和&var1);
如果(*ptr1>0)
printf(“\n输入参数:\n”);
func_id current_func;
对于(i=0;i我可能会阅读变量函数。它们会让你做你想做的。然后我会重写所有函数,以便它们总是在void*
中传递变量参数,也可能总是返回值
如果使用变量参数方案,您可能只需要一个函数:我没有仔细阅读您的代码以确定这一点。然后您可以将(int)id
作为第一个参数传递,以明确地告诉函数要做什么以及如何做
您的声明将如下(未经测试):
#包括
空虚*
_func(int-id,void*arg1,…);
你的定义是这样的(未经测试):
#包括
空虚*
_func(int-id,void*arg1,…){
#定义最大参数10;
int i=0;
void*args[max_args];
va_列表arglist;
...
//提取传入参数
va_启动(arglist,id);
而(i++
你确定C语言是正确的吗?不幸的是,我不得不使用C语言。我没有详细阅读所有内容,但来回将int
转换为void*
肯定不是一个好主意。它们可能有不同的宽度(例如,在amd64上)。如果必须执行这些操作,请始终使用intptr\u t
。并且函数具有不同的返回类型也特别危险。体系结构可能有特殊约定,用于在不同寄存器中返回特定类型的值。在这种情况下,最好将指向返回类型的指针作为参数传递,并将结果有。你想要的函数重载吗?谢谢Pete的建议。但我不能更改函数,基本上我需要一种机制,通过该机制,我可以调用任何一个函数,其参数被放入定义的内存位置,并通过适当的类型转换读取它们,执行函数并放置结果(如果有)在另一个定义的内存位置上(使用适当的类型转换)。您不能更改它们,但可以将它们包装起来,以使它们正好适合一个函数逻辑方案。