Array of pointers to functions
dan at BBN-PROPHET.ARPA
dan at BBN-PROPHET.ARPA
Tue Apr 8 07:29:29 AEST 1986
Re array of pointers to functions: the way I think of it is that C
declarations must be read "from the inside out". Start with the
identifier itself, and work your way out from the tightest-binding
declaration operator to the loosest. The first declaration operator you
hit gives you the fundamental type of the object. So, given a declaration
containing
... (*x) ...
No matter what is surrounding the "(*x)", x is a pointer. Always.
Never an array, function, etc., though what it points to may involve
these. On the other hand, given
... (x[5]) ...
X is definitely an array of 5 (somethings). Maybe pointers, ints, etc.
So, to construct an array of pointers, you say
(x[])
to get the array, and add the '*' next:
(*(x[]))
Now it's an array of pointers. What do they point to? Functions:
( (*(x[])) () )
Now all you need to do is specify what the functions return:
int ( (*(x[])) () )
And you're done. However, you've left behind a pretty confusing
declaration. C typedefs are the perfect way to make things like this easy
to understand:
typedef int FUNC();
typedef FUNC * FUNC_PTR;
FUNC_PTR x[];
Now it's quite clear that x is an array of function pointers. I usually
use some variation on this when I have to construct such arrays; it's a
lot clearer, even to people like me who have been using C for years.
Hope this helps.
Dan Franklin
More information about the Comp.lang.c
mailing list