1

Is it possible to define an array of function pointers (and the functions don't have se same input argument ) as indicating in the following code ?

If yes what I have to put in the function definition int (*handler)(/*what Ihave to put here ?*/);

struct handler_index {
    const char *name;
    int (*handler)(/*what Ihave to put here ?*/);
};

int handler0 (int a, int b)
{
    printf("%d\n",a+b);
}

int handler1 (int a, int b, int c)
{
    printf("%d\n",a+b+c);
}

int handler2 (int a, int b, int c, int d)
{
    printf("%d\n",a+b+c+d);
}

const struct handler_index handler_index[] = {
  [0] = {"handler0", handler0},
  [1] = {"handler1", handler1},
  [2] = {"handler2", handler3},
};
2
  • @BoPersson the handler functions don't have the same number of argument and I want to know how to define the function pointer for this case to support all handler functions Commented Dec 27, 2012 at 7:24
  • OK, I needed that in a bigt project. and I could not communicate the details Commented Dec 27, 2012 at 12:14

3 Answers 3

5

Just put nothing:

int (*handler)();

it means the function has an unspecified (but non-variable) number and types of parameters.

Any function that returns an int and with a fixed variable number of parameters can be assigned to handler.

Sign up to request clarification or add additional context in comments.

4 Comments

what you mean with (but non-variable) ?
He means, that this functions can not take variable list of arguments.
Could you please provide example of function definition (but non-variable) that I could not use with int (*handler)()
@MohamedKALLEL not a function declared with a trailing ellipsis notation. e.g., not int foo(char *, ...);
3

Whilst int (*handler)() will indeed allow variable number of arguments for th function, I fail to see any benefit in this. Function pointers are useful when you have a piece of code that takes something, finds the "right thing to do" (e.g comparing the "name" with some input from elsewhere), and calls the function pointer to do whatever it has to do. The function pointer calling code needs to know how many arguments the function has (how else would it pass the right number and order of arguments.

I don't actually see any meaningful use of this at all. Yes, you can pass a variable number of arguments to a function, but the code HAS to know what arguments the function takes.

Unless the arguments are somehow specified in the definition of the struct - but then you need to define different content for the struct to allow for that.

I would suggest that you need to think about what you are trying to achieve, and then come up with a solution to the problem, most likely using a different method.

4 Comments

I actually came across this question and think my use case makes a lot of sense: Pipelines. I'm trying to emulate something like python sklearn's pipelines in C, where a pipeline consists of a series of functions that each perform some operation.
Well, yes, but the calling code still needs to know what to pass, and the called code needs to know how to use those arguments. If you re-order the pipeline, the code that calls the functions for the ppeline also needs to know that it has changed. A better approach would be to have a pipeline baseclass, and then derive from that class to perform the different pipeline stages.
There's no classes in C though, and with such a framework you rely on the developer knowing what order the steps are in and knowing that their functions return something that can be used at the next step. GRPPI is a really good example of this sort of thing for c++ that does exactly this
In C you can use a struct with function pointers - the struct also holds the relevant data. And it's much safer. Say we replicate an assembly-line of cars, and the struct is the build instructions for a given car. We can re-order the fit_wheels and fit_engine, and since the arguments are part of the struct, you don't need to change the engine_size and wheel_style parameters. Having an array with just functions that take arbitrary parameters doesn't allow you to do anything flexible.
0

Put nothing. Just empty brackets.

int (*handler)();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.