Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array of pointers to functions

Tags:

c

i need to write a function that receives an array of pointers to functions. i wrote the following code, however i'm having trouble in testing it at the moment.

is this the correct way to define a pointers to function array?

typedef (*Function)(double);
void func(Function* arr);

and if i want to declare the size of the array with [20] do i write:

void func(Function arr[20]);

?

thanks for your help

like image 867
Mike Avatar asked Sep 02 '10 15:09

Mike


People also ask

Can we have an array of function pointers?

4) Like normal pointers, we can have an array of function pointers. Below example in point 5 shows syntax for array of pointers. 5) Function pointer can be used in place of switch case.

What is array of function pointer?

Array of Function PointersWe declare and define four functions which take two integer arguments and return an integer value. These functions add, subtract, multiply and divide the two arguments regarding which function is being called by the user.

How do you pass an array to a function with pointer?

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().

Can you pass pointers to functions?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.


1 Answers

First, my usual method for figuring out complex types. Start with the identifier, and then add on the rest one step at a time:

    f          -- f
    f[N]       -- is an N-element array
   *f[N]       -- of pointers
  (*f[N])()    -- to functions
T (*f[N])()    -- returning T

For an array of pointers to functions taking a double parameter and returning a double value, that would be

double (*f[N])(double);

However, remember that expressions of array type "decay" from type "N-element array of T" to "pointer to T" in most contexts. When you pass an array expression as an argument to a function, what the function actually receives is a pointer. So, instead of receiving an object of type "N-element array of pointer to function returning double", your function will receive an object of type "pointer to pointer to function returning double", or

double (**f)(double)

So your function definition would look something like

void func(double (**f)(double))
{
  int i;
  ...
  for (i = 0; f[i] != NULL; i++)
  {
    double x = (*f[i])((double) i);
  }
}

And the caller would look something like

double a(double x) {...}
double b(double x) {...}
double c(double x) {...}

void foo(void)
{
  double (*list[])(double) = {a, b, c, NULL};
  func(list);
}

If you want to use typedefs instead, you could use something like this:

typedef double Dblfunc(double);   // typedef for function type 
typedef Dblfunc *Dblfuncptr;      // typedef for function pointer type

void func(Dblfuncptr *f)
{
  int i;
  for (i = 0; f[i] != NULL; i++)
  {
    double x = (*f[i])((double) i);
    ...
  }
}
...
void foo(void)
{
  Dblfuncptr list[] = {a, b, c, NULL}; // EDIT: fixed type
  func(list);
}

Using the typedefs makes the array and function parameter look more like regular types. Personally, I prefer using the "raw" types, since it shows explicitly that I'm dealing with pointers to functions, and it shows what the return and parameter types are.

like image 199
John Bode Avatar answered Sep 28 '22 01:09

John Bode