如何制作一个指针数组来调用func指针?

如何制作一个指针数组来调用func指针?,c,C,我有一个func指针数组的代码 #include <stdio.h> int sum(int a, int b); int subtract(int a, int b); int mul(int a, int b); int div(int a, int b); int (*p[4]) (int x, int y); int main(void) { int result; int i, j, op; p[0] = sum; /* address of sum()

我有一个func指针数组的代码

#include <stdio.h>

int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);

int (*p[4]) (int x, int y);

int main(void)
{
  int result;
  int i, j, op;

  p[0] = sum; /* address of sum() */
  p[1] = subtract; /* address of subtract() */
  p[2] = mul; /* address of mul() */
  p[3] = div; /* address of div() */

  printf("Enter two numbers: ");
  scanf("%d %d", &i, &j);

  printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
  do {
    printf("Enter number of operation: ");
    scanf("%d", &op);
  } while(op<0 || op>3);

  result = (*p[op]) (i, j);
  printf("%d", result);

  return 0;
}

int sum(int a, int b)
{
  return a + b;
}

int subtract(int a, int b)
{
  return a - b;
}

int mul(int a, int b)
{
  return a * b;
}

int div(int a, int b)
{
  if(b) 
      return a / b;
  else 
      return 0;
}
#包括
整数和(整数a,整数b);
整数减去(整数a,整数b);
int mul(int a,int b);
内部分区(内部a、内部b);
int(*p[4])(int x,int y);
内部主(空)
{
int结果;
int i,j,op;
p[0]=sum;/*sum()的地址*/
p[1]=subtract;/*subtract()的地址*/
p[2]=mul;/*mul()的地址*/
p[3]=div;/*div()的地址*/
printf(“输入两个数字:”);
scanf(“%d%d”、&i和&j);
printf(“0:Add,1:Subtract,2:Multiply,3:Divide\n”);
做{
printf(“输入操作编号:”);
scanf(“%d”和&op);
}while(op3);
结果=(*p[op])(i,j);
printf(“%d”,结果);
返回0;
}
整数和(整数a,整数b)
{
返回a+b;
}
整数减法(整数a,整数b)
{
返回a-b;
}
int mul(int a,int b)
{
返回a*b;
}
内部分区(内部a、内部b)
{
如果(b)
返回a/b;
其他的
返回0;
}
指向函数的指针数组的代码:

#include <stdio.h>

int sum(int, int);
int product(int, int);
int subtract(int, int);

int main()
{
   int i = 0;
   int a = 10;
   int b = 5;
   int result = 0;
   int (*pfun[3])(int, int);

   pfun[0] = sum;
   pfun[1] = product;
   pfun[2] = subtract;

   for( i = 0 ; i < 3 ; i++)
   {
     result = pfun[i](a, b);
     printf("\nresult = %d", result);
   }

   result = pfun[1](pfun[0](a, b), pfun[2](a, b));
   printf("\n\nThe product of the sum and the subtract = %d\n",result);
}

int sum(int x, int y)
{
   return x + y;
}

int product(int x, int y)
{
   return x * y;
}

int subtract(int x, int y)
{
   return x - y;
}
#包括
整数和(整数,整数);
int产品(int,int);
int减法(int,int);
int main()
{
int i=0;
INTA=10;
int b=5;
int结果=0;
int(*pfun[3])(int,int);
pfun[0]=总和;
pfun[1]=产品;
pfun[2]=减法;
对于(i=0;i<3;i++)
{
结果=pfun[i](a,b);
printf(“\nresult=%d”,结果);
}
结果=pfun[1](pfun[0](a,b),pfun[2](a,b));
printf(“\n\n总和与减法的乘积=%d\n”,结果);
}
整数和(整数x,整数y)
{
返回x+y;
}
整数乘积(整数x,整数y)
{
返回x*y;
}
整数减去(整数x,整数y)
{
返回x-y;
}

现在如何结合这两个程序。这样,指向func指针的指针数组和func指针可能具有不同数量的参数?任何建议。

您不仅需要存储参数数量可变的函数指针(这不是很难,您可以使用
联合
),而且还需要确保使用正确的参数调用函数,考虑到您的设计,这有点棘手

我建议用一个新的。所有函数都只将堆栈作为参数:

void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
您的数组可以这样声明:

typedef void callback_t(stack_t *);

callback_t *p[] =
{
    sum,
    subtract,
    product,
    /* ... */
};
void neg(stack_t *stack)
{
    if (depth(stack) < 1)
        perror("Not enough arguments in stack!");
    int a = popstack(stack);
    pushstack(stack, -a);
}
例如,
sum
将被实现为:

void sum(stack_t *stack)
{
    if (depth(stack) < 2)
        perror("Not enough arguments in stack!");
    int b = popstack(stack);
    int a = popstack(stack);
    int c = a + b;
    pushstack(stack, c);
}
void sum(stack\u t*stack)
{
if(深度(堆栈)<2)
perror(“堆栈中的参数不足!”);
int b=popstack(堆栈);
int a=popstack(堆栈);
INTC=a+b;
推式堆栈(堆栈,c);
}
但一元负号的实现方式如下:

typedef void callback_t(stack_t *);

callback_t *p[] =
{
    sum,
    subtract,
    product,
    /* ... */
};
void neg(stack_t *stack)
{
    if (depth(stack) < 1)
        perror("Not enough arguments in stack!");
    int a = popstack(stack);
    pushstack(stack, -a);
}
void neg(堆栈)
{
if(深度(堆栈)<1)
perror(“堆栈中的参数不足!”);
int a=popstack(堆栈);
推堆栈(堆栈,-a);
}

每个函数决定它们需要多少参数。调用方不需要知道。

您不仅需要存储参数数量可变的函数指针(这不是很难,您可以使用
联合
),而且还需要确保使用正确的参数调用函数,考虑到您的设计,这有点棘手

我建议用一个新的。所有函数都只将堆栈作为参数:

void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
您的数组可以这样声明:

typedef void callback_t(stack_t *);

callback_t *p[] =
{
    sum,
    subtract,
    product,
    /* ... */
};
void neg(stack_t *stack)
{
    if (depth(stack) < 1)
        perror("Not enough arguments in stack!");
    int a = popstack(stack);
    pushstack(stack, -a);
}
例如,
sum
将被实现为:

void sum(stack_t *stack)
{
    if (depth(stack) < 2)
        perror("Not enough arguments in stack!");
    int b = popstack(stack);
    int a = popstack(stack);
    int c = a + b;
    pushstack(stack, c);
}
void sum(stack\u t*stack)
{
if(深度(堆栈)<2)
perror(“堆栈中的参数不足!”);
int b=popstack(堆栈);
int a=popstack(堆栈);
INTC=a+b;
推式堆栈(堆栈,c);
}
但一元负号的实现方式如下:

typedef void callback_t(stack_t *);

callback_t *p[] =
{
    sum,
    subtract,
    product,
    /* ... */
};
void neg(stack_t *stack)
{
    if (depth(stack) < 1)
        perror("Not enough arguments in stack!");
    int a = popstack(stack);
    pushstack(stack, -a);
}
void neg(堆栈)
{
if(深度(堆栈)<1)
perror(“堆栈中的参数不足!”);
int a=popstack(堆栈);
推堆栈(堆栈,-a);
}

每个函数决定它们需要多少参数。打电话的人不需要知道。

你告诉我!你在写什么语言?那你为什么写C++标签?你告诉我!“你写的是哪种语言?那你为什么写C++标签?”图克:我不确定我理解这个问题,但是我把我认为是你需要的信息加进去了。告诉我这是否是你想要的。@thuk:我不确定我是否理解这个问题,但我补充了我认为是你需要的信息;告诉我这是不是你想要的。