Array of pointers to functions

der Mouse mouse at mcgill-vision.UUCP
Sat Apr 12 17:11:52 AEST 1986


Well, I  tried to mail this, but the percent sign  got @ified  somewhere
along  the  way,  then  some later  mailer  bounced it because of  that.
Besides, this may help someone else,  or at  least so I  rationalize  to
myself.

> I'm confused.  How do you specify an array of pointers to functions?  Do
> you do it:
>            int (*foo[])();
This is right.
> or do you do it:
>            int ((*foo)())[];
This specifies  a  pointer to a function returning an  array of ints, if
read  literally.    Since  functions  cannot return  arrays (as  current
compilers stand), this is nonsense, but that's what it says.

> (after all, a pointer to a function is 'int (*foo)();'; thus, an array of
> them should be done by adding the '[]' at the end).
> Yet, the C compiler I'm using wants it the first way and the logic excapes
> me as to why.

     Here is  a  technique which  I have found  helps  with  complicated
declarations; it  consists of  building  the declaration one  piece at a
time, parenthesizing fully as you go.  In this case:

Foo should be...
	         foo
...an array of...
	        (foo)[]
...pointers to...
	      *((foo)[])
...functions returning...
	     (*((foo)[]))()
...int
	int ((*((foo)[]))())

This is a valid declaration and will work.   Some of the parentheses can
be stripped out, however:

	int ((*((foo)[]))())
() binds tighter than the `int', get rid of one pair
	    x              x
	int  (*((foo)[]))()
() binds tighter than *, keep the next pair
	     x          x
	int  (*((foo)[]))()
[] binds tighter than *, get rid of the next pair
	       x       x
	int  (* (foo)[] )()
The innermost pair is totally unnecessary
	        x   x
	int  (*  foo [] )()
and we have the original declaration.  If  you are not sure which of two
things binds tighter, well, it can never hurt  to  leave the parentheses
in, so when in doubt.....

     I have  yet to  see a type which will not  yield to this technique.
Of course, some types do not exist, such as arrays of functions,
	int foo[]();
or functions returning arrays,
	int foo()[N];
or functions returning functions,
	int foo()();
though functions returning pointers to functions are OK.
	int (*foo())();
The declarations can be written, however.

     You were almost right in talking about throwing  [] on  the end  of
the one to get the other; what you need to do is throw the [] on the end
of  the variable (possibly  with parentheses), not the end of  the whole
declaration.
-- 
					der Mouse

USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse
     philabs!micomvax!musocs!mcgill-vision!mouse
Europe: mcvax!decvax!utcsri!mcgill-vision!mouse
        mcvax!seismo!cmcl2!philabs!micomvax!musocs!mcgill-vision!mouse
ARPAnet: utcsri!mcgill-vision!mouse at uw-beaver.arpa

Wizard:  One who can find and fix bugs in an emergency
	(such as during a site visit by funding agencies).



More information about the Comp.lang.c mailing list