4

Following is the array of function pointers

(int) (*a[5]) (int);
 int f1(int){};
 ...

is the following way of definition correct?

a = f1;
a + 1 = f2;
a + 2 = f3; 
...

how do we call these functions?

*a(1) // is this correct???
*(a+1) (2)
BenMorel
  • 34,448
  • 50
  • 182
  • 322
codey modey
  • 983
  • 2
  • 10
  • 23
  • 3
    What did your compiler say? I suspect it complained about an `rvalue` where it expected an `lvalue`??? – Floris Dec 16 '13 at 05:46
  • [Some more clarification is available here.](http://stackoverflow.com/questions/252748/using-an-array-of-function-pointers) – Dayal rai Dec 16 '13 at 05:56

5 Answers5

2
#include <stdio.h>

int f1(int i) { return i; }
int f2(int i) { return i; }

int main() {
    int (*a[5]) (int);
    a[0] = f1;
    a[1] = f2;
    printf("%d\n", a[0](2));
    printf("%d\n", a[1](5));
}
Oleg Razgulyaev
  • 5,757
  • 4
  • 28
  • 28
2

What you call "definition" is just assignment, and as you are doing it, it is wrong, since you can't assign to arrays in C. You can only assign to individual array elements, correct would be a[0] = f1 etc.

Often for arrays of function pointers there is no need to assign them dynamically at run time. Function pointers are compile time (or link time) constants anyhow.

/* in your .h file */
extern int (*const a[5]) (int);

/* in your .c file */
int (*const a[5]) (int) = { f1, f2, f3 };

To simplify using function pointers a bit, the identifier for a f1 is equivalent to a pointer to the function &f1 and using a function pointer with parenthesis as in a[0](5) is the same as dereferencing the pointer and calling the resulting function (*(a[0]))(5).

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • I am still fighting to understand how is a[0](5) equivalent to (*(a[0]))(5) – codey modey Dec 16 '13 at 06:43
  • @user2901020, this is so for any function pointer `int (*fp)(int)`, `fp(5)` and `(*fp)(5)` are equivalent. There is nothing to "understand" here, this is just a short cut defined by the C language. – Jens Gustedt Dec 16 '13 at 06:56
1

you can write:

a[0]=&f1;

and call it as below:

a[0](1);

note that there is no need to use a pointer while the function is getting called. If you insist on using a pointer, then you can anyhow do the below:

(*a[0])(1);
Vijay
  • 65,327
  • 90
  • 227
  • 319
1

It is always better to use typedefs, (and all your functions need to have a common interface anyway and also to initialise at declaration time, where possible function arrays should also be const as a safety measure so:

#include <stdio.h>

typedef int (*mytype)();
int f1(int i) { return i; }
int f2(int i) { return i; }

int main() {
    const mytype a[5] = {f1,f1,f2,f2,f1};

    printf("%d\n", a[0](2));
    printf("%d\n", a[1](5));

    return 0;
}
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
0
#include<stdio.h> 
int (*a[5]) (int);
int f1(int i){printf("%d\n",i); return 0;}
int f2(int i){printf("%d\n",i); return 0;}
int f3(int i){printf("%d\n",i); return 0;}

int main(void)
{
    a[0] = f1;  // Assign the address of function
    a[1] = f2;
    a[2] = f3;

    (*a[0])(5);  // Calling the function
    (*a[1])(6);
    (*a[2])(7);

    getchar();
    return 0;
}