简体   繁体   中英

How can I make a function pointer that points to functions with different number of arguments?

I am trying to implement a simple function pointer program, but I am getting this warning:

Warning: assignment from incompatible pointer type while assigning function address to function pointer

My program is as below:

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int add(int a, int b);
int sub(int a, int b);
int Add3Num(int a, int b, int c);

int main(int argc, char *argv[]) {
  int (*func)(int , ...);
  int a = 20, b = 10, c = 5, result;

  func = add;
  result = func(a, b);  // Warning over here
  printf("Result of 20 + 10 = %d\n", result);

  func = sub;
  result = func(a, b);  // Warning over here
  printf("Result of 20 - 10 = %d\n", result);

  func = Add3Num;
  result = func(a, b, c);  // Warning over here
  printf("Result of 20 + 10 + 5 = %d\n", result);

  return 0;
}

int add(int a, int b){
  return a+b;
}
int sub(int a, int b){
    return a-b;
}
int Add3Num(int a, int b, int c){
  return a+b+c;
}

Declare the pointer as a pointer to a prototypeless function (a function taking an unspecified number of arguments):

  int (*func)();

Then your program should work, without any need for casts (as long as each call then continues to match the function being pointed at at the moment).

#include <stdio.h>
int add(int a, int b);
int sub(int a, int b);
int Add3Num(int a, int b, int c);
int main(){
  int (*func)(); /*unspecified number of arguments*/
  int a = 20, b = 10, c = 5, result;

  /*Casts not needed for these function pointer assignments
    because: https://stackoverflow.com/questions/49847582/implicit-function-pointer-conversions */
  func = add;
  result = func(a, b);
  printf("Result of 20 + 10 = %d\n", result);

  func = sub;
  result = func(a, b);
  printf("Result of 20 - 10 = %d\n", result);

  func = Add3Num;
  result = func(a, b, c);
  printf("Result of 20 + 10 + 5 = %d\n", result);
  return 0;
}
/*...*/

With prototyped functions, the match between function pointer types needs to be more or less exact (with some caveats like that top level qualifiers don't matter or that things can be spelled out differently), or else the standard leaves things undefined.

Alternatively, and perhaps preferably given that prototypeless functions and function pointer are an obsolescent feature, you can use strongly typed pointers (first int (*)(int,int) and then int (*)(int,int,int) ) and use casts to force things.

#include <stdio.h>
int add(int a, int b);
int sub(int a, int b);
int Add3Num(int a, int b, int c);
int main(){
  int (*func)(int , int);
  int a = 20, b = 10, c = 5, result;

  func = add;
  result = func(a, b);
  printf("Result of 20 + 10 = %d\n", result);

  func = sub;
  result = func(a, b);
  printf("Result of 20 - 10 = %d\n", result);

  /*cast it so it can be stored in func*/
  func = (int (*)(int,int))Add3Num; 
  /*cast func back so the call is defined (and compiles)*/
  result = ((int (*)(int,int,int))func)(a, b, c); 
  printf("Result of 20 + 10 + 5 = %d\n", result);
  return 0;
}
/*...*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM