2

Related to [question]: How do you pass a function as a parameter in C?

Is it possible in C to pass a function that has a variable number of arguments to another function? If so, could someone point me to some documentation or fill me in? Thanks.

Community
  • 1
  • 1
Vistian
  • 29
  • 3

5 Answers5

3

You can't pass a function (of any sort) as a parameter, but you can pass a pointer to a function (again, of pretty much any sort). It's usually easiest to use a typedef:

typedef int (*fptr)(char const *, ...); // e.g., match with `printf`

int apply(fptr f, char const *a, int b) { 
    return f(a, b);
}
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
3

You can make a function pointer, e.g.:

typedef int (*vafunc)(const char *, ...); // like printf

However, you cannot really forward the arguments, i.e. the following doesn't exist in standard C:

void call_other(vafunc f, const char * fmt, ...)
{
    // want to say:
    // f(fmt, ...);  // How to write this???
}

GCC offers such anonymous argument forwarding as an extension, but it's not possible in standard C. You're typically expected to match each variadic function with a v... counterpart that takes a va_list argument, precisely for this purpose. (Calling f with a fixed number of arguments is possible, of course: f("abc", 1, 2, 3);)

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
0

http://www.lemoda.net/c/function-pointer-ellipsis/index.html seems to be what you seek.

Rafe Kettler
  • 69,672
  • 18
  • 145
  • 147
0

You can pass a varargs function the same way as you would another function.

Here's a short test program that demonstrates passing a varargs function as a parameter:

int bar(int x, ...) {
    /* In a real program, you would actually use the arguments beyond x */
    return x;
}

int foo(int (*baz)(int, ...)) {
    return bar(10, "ABC");
}

int main(void) {
    printf("%d\n", foo(bar));
    return 0;
}

It prints 10, as you might expect.

N.B. You can't actually pass a function as an argument to another function in C - instead, you can pass a function pointer, which points to the actual function as loaded from the executable. This is weaker than the first-class status of functions in functional programming languages, or even than the status of a delegate in some object-oriented languages like C#. For instance, you can't create a function on the fly in C.

Adam Mihalcin
  • 13,346
  • 3
  • 29
  • 49
0

Sure. They're called "variadic functions".

1) Just use an "ellipses" ("...") as the last argument in your function prototype

2) In order for the called function to "know" how many arguments were passed to it, use the macros "va_start" and friends in "stdargs":

3) 1) and 2) above are simply to have a function that has a variable #/arguments. You can also have a function pointer with a variable #/arguments. Do the same thing - just include the ellipses ("...") in your function prototype, and use "stdargs" in your function implementation.

paulsm4
  • 99,714
  • 15
  • 125
  • 160