0

It might be impossible to do but can we actually get the number of arguments a function's wants in C?

The goal is to make a function named call that requires a function's pointer and some arguments. But because we can't count the number of arguments in a variadic arguments function, I want to count how many arguments does the function's pointer requires.

Here's how I want to create it:

void func(int a, int b, char* c) {
    // Do things
}

int call(void *f, ...) {
    // Find how much arguments *f wants and loop over the variadic args
}

int main() {
    call(func, 1, 2, "hello");
}

By the way, it needs to be at run-time.

Nathanael Demacon
  • 1,169
  • 7
  • 19
  • No. But C has va_start, va_args. – stark Jan 03 '19 at 14:12
  • 1
    Is your question about getting this information at run-time, or compile time? – ryyker Jan 03 '19 at 14:15
  • 2
    There are probably better ways to do what you want. Do you have a specific use case in mind? – dbush Jan 03 '19 at 14:15
  • 3
    You also need to worry about the types of the function's parameters and possibly the function's return type. – Ian Abbott Jan 03 '19 at 14:17
  • 1
    At runtime, an executable or .dll can be programmatically loaded, and _read_, and some information describing function decoration can be determined, but this is probably beyond the scope of what you are looking for. – ryyker Jan 03 '19 at 14:27
  • 2
    No, standard C has no facility for determining the number or types of arguments a function requires, given a pointer to that function (much less a pointer to `void`), nor any way of meaningfully representing that information. An individual implementation *may* offer such a facility, but I've never seen or heard of one. – John Bode Jan 03 '19 at 14:27
  • If the intent is for `call` to call your function, why not pass it the `va_list` and let it handle what parameters it needs – Chris Turner Jan 03 '19 at 14:46
  • I want it to be used by just declaring the function and using call, no more things must be needed in the function side, just the call function. – Nathanael Demacon Jan 03 '19 at 15:37

1 Answers1

0

Nothing in the C standard provides a facility for ascertaining the number of arguments a function requires, other than the fact that C implementations are free to provide extensions.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312