Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site dg_rtp.UUCP Path: utzoo!linus!decvax!mcnc!rti-sel!dg_rtp!throopw From: throopw@dg_rtp.UUCP (Wayne Throop) Newsgroups: net.lang.c Subject: Re: Array of pointers to functions Message-ID: <290@dg_rtp.UUCP> Date: Wed, 9-Apr-86 18:15:08 EST Article-I.D.: dg_rtp.290 Posted: Wed Apr 9 18:15:08 1986 Date-Received: Fri, 11-Apr-86 07:30:13 EST References: <2398@brl-smoke.ARPA> Lines: 73 > 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@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 !mcnc!rti-sel!dg_rtp!throopw