declaring routines which return function pointers

Morris Keesan keesan at bbncca.ARPA
Wed Dec 19 01:32:17 AEST 1984


----------------
USE TYPEDEFS!!

Sure, you can use "longhand", and declare f as a pointer to function returning
int, and declare fa as a function returning a pointer to function returning
int, but even if you know what you're doing [ basic rule -- as you pronounce
the English from left to right, build the declaration from the innermost
parentheses out ] and manage to correctly build the declarations

    int (*f)();
    int (*(fa()))();    /* Yes, I know there's a redundant pair of () */

you've got code which the next person will have to spend five minutes
deciphering, even with comments.  Compare the above with

    typedef (*PFI)();   /* Pointer to function returning int */
    PFI f, fa();

Why would anyone choose the former?  This, to my mind, is the major beauty of
typedefs.  Information hiding is all well and good, but can be accomplished by
judicious use of #defines, without typedefs.  Using typedefs allows you to
break down complex type declarations into manageable chunks, providing much
more readable code, and much less chance of getting the declaration wrong in
the first place.
-- 
			    Morris M. Keesan
			    {decvax,linus,ihnp4,wivax,wjh12,ima}!bbncca!keesan
			    keesan @ BBN-UNIX.ARPA



More information about the Comp.lang.c mailing list