0

I'm trying to make a program that receives a pointer to a function and the arguments, and then calls the function with the arguments. The best way to do it seems to be passing the arguments as an array, but I can't change the arguments accepted by the function.

This is similar to what I would like to do.

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

int add3(int a, int b, int c){
    return a + b + c;
}

int main(){
    int* args = calloc(sizeof(int) * 2);
    add(args);
    args = calloc(sizeof(int) * 3);
    add3(args);
}
Ben
  • 13
  • 5
  • 1
    Then why doesn't your function accept `int *` as an argument? – Cheatah Nov 05 '22 at 21:51
  • My code is supposed to take a pointer to a function, then a variable number of arguments, and then call the function with the arguments. I can't change the arguments to the function. – Ben Nov 05 '22 at 21:53
  • Your function `add` is not declared as accepting an array; it's declared as taking two separate ints. In addition, your `main` makes no sense. You allocate a block of memory that is the size of two ints, which leaves you with a pointer to a block of memory. You don't assign anything to that memory, but expect to pass it to a function that accepts two separate ints and expect it to return a meaningful value. You need to go back to the book chapter or your notes on functions. – Ken White Nov 05 '22 at 22:06
  • So what you want is precluded because of your requirements. You can't pass an array to a function that does not accept one as an argument. – Cheatah Nov 05 '22 at 22:12
  • Are you talking about variable arguments, `#include , int add(int, ...);`. See https://c-faq.com/varargs/index.html. – Neil Nov 05 '22 at 22:25
  • No. I have a library with functions that take a fixed number of inputs. I want to be able to call those functions from a pointer and pass the arguments. The two options for calling the function seem to be either writing a different calling function per input types or somehow calling the functions with an array of the values. I can't change the function inputs – Ben Nov 06 '22 at 11:53
  • 1
    It sounds like you can just use `add(args[0], args[1])` based on your example. If your problem is more complicated, you may need to update your example to include a bit more of the complexity. See https://stackoverflow.com/help/minimal-reproducible-example. – Shane Bishop Nov 06 '22 at 17:50
  • How many arguments does the function that you are supposed to be calling take? As @Shane-Bishop pointed out, your example doesn't reflect the complexity of the problem – Ahmed Masud Nov 06 '22 at 20:48
  • 1
    Re “The best way to do it seems to be…”: What it seems like to you is not what it is. There is nothing apparent in the code shown that calls for `add` or `add3` to be passed an array. Either call `add` with `add(args[0], args[1])` or write new routine `int addA(int *array) { return add(array[0], array[1]); }` and `int add3A(int *array) { return add3(array[0], array[1], array[2]); }` or give more information showing why it is beneficial to do something else. – Eric Postpischil Nov 06 '22 at 20:49

0 Answers0