Array of pointers to functions

Wayne Throop throopw at dg_rtp.UUCP
Thu Apr 10 09:15:08 AEST 1986


> I'm confused.  [Is] an array of pointers to functions [declared like this]?
>            int (*foo[])();
> [or like this?]
>            int ((*foo)())[];
> [...]
> I'm really confused about this point which is (as far as I can tell) not
> in K&R.
>     William Woody    Woody%Romeo at Hamlet.Caltech.Edu

Actually, it *is* in K&R, though in an unexpected place.  Page 90.
(Ahem.)

    [...] the syntax of the declaration for a variable mimics the syntax
    of expressions in which the variable might appear.

Thus, your second example is indirected, then invoked, then subscripted,
so it is a pointer to a function returning an array (that is, really a
function returning a pointer, though this point is obscure).  Your first
example is subscripted, then indirected, then invoked, so it is an array
of pointers to functions.  I always fully parenthesize declarations, so
that I'd prefer the first to be "int (*(foo[]))();", just so that the
order is less likely to be mistaken.

In general, to construct any compound type involving invocation
(function returning), indirection (pointer to), or subscription (array
of), start from the name, and build outward.  For example, let's
construct a declaration of an array of pointer to int.

                foo[]                   array of
                *(foo[])                pointer to
        int     *(foo[]);               int

Or construct a declaration of a function returning pointer to char

                foo()                   function returning
                *(foo())                pointer to
        char    *(foo());               char

The construction method is simple.  As you read through the description
of what you want, just take the previous stuff, parenthesize (to avoid
ambiguity), add the new operation to the outside, and top it off with
the primitive type.  Thus, the example of array of pointer to function
returning int becomes

                foo[]                   array of
                *(foo[])                pointer to
                (*(foo[]))()            function returning
        int     (*(foo[]))();           int

Note that reading one of these declarations aloud is easy as well.  Just
start from the name, and read the operators that are applied in turn
aloud until you get to the primitive type.  "*" is read as "pointer
to", "[]" as "array of", and "()" as "function returning", and grouping
parenthesis are silent.  For example,

    in this declaration:             int   *((*(foo[]))());
    operations are in this order:          4  2    1   3

so it is an array of pointer to function returning pointer to int.


Now, this is one of those recurring topics.  I'll agree that C's type
declaration syntax is peculiar, and hard to follow at first glance.  But
I do wonder.... just why does anybody think that "int ((*foo)())[];" is
an array of pointer to function?  The obscurity of the explanatory K&R
passage aside, just what do you folks *do* when you try to construct a
declaration in C?  This question is *not* rhetorical, nor intended as an
insult.... I'd really like to know how folks go about constructing
variable and type declarations in C.  Please mail me your methods...
I'll post a summary if the response warrants it.
-- 
Wayne Throop at Data General, RTP, NC
<the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list