[comp.sys.amiga.tech] Prototypes...

ecphssrw@solaria.csun.edu (Stephen Walton) (12/07/88)

Here's a handy hint for people who need to maintain code both on
compilers with and without prototypes.  I've been meaning to post it
for a while, and Mike Meyers's posting reminded me. It comes from
Thomas Plum's excellent book _Reliable Data Structures in C_.  And no,
you Modula hackers, this title is not an oxymoron :-) . 
   The basic idea is to wrap prototype declarations in a declaration
called, for the example, PARMS.  For instance:

void *AllocMem PARMS((ULONG size, ULONG flags));

Note the double parens.  We can then do:

#if HAVE_PROTOTYPES
#define PARMS(x) x
#else
#define PARMS(x) ()
#endif

which replaces the PARMS macro with a prototype or with a set of empty
parens.  I use Manx 3.6a but also have Lint, so I use the above
convention, where HAVE_PROTOTYPES is #ifdef'd on _lint_. 
-- 
Stephen Walton, Dept. of Physics & Astronomy, Cal State Univ. Northridge
RCKG01M@CALSTATE.BITNET       ecphssrw@afws.csun.edu
swalton@solar.stanford.edu    ...!csun!afws.csun.edu!bcphssrw

mwm@raven.pa.dec.com (Mike (Real Amigas have keyboard garages) Meyer) (08/04/90)

In article <1343@metaphor.Metaphor.COM> djh@neuromancer.metaphor.com (Dallas J. Hodgson) writes:

   In article <115524@linus.mitre.org> duncant@mbunix.mitre.org (Thomson) writes:
   >Oops, I just realized that in my last posting on this subject, I 
   >forgot that |Read() and Write() are not stdio functions, but amiga
   >functions.  But the same argument follows if the file which 
   >contained prototypes for Read() and Write() had been included.

   No, no, no! The purpose of include files is to specify the way arguments
   OUGHTTA look, not to COERCE them into something. How would you catch any
   lint warnings if, for example, you past a short to an amiga lib function
   expecting a 32-bit pointer?

Wrong answer - just slightly. The purpose of prototypes is to say what
the arguments ought to look like, and where reasonable, coerce them
into the right type. A function call consists of of a bunch of
assignments; they act just like assignments, including coercions.

If the coercions are "wrong" - i.e, any integral type (whether 16 or
32 bits) to any pointer type, or a long into a short - you should get
a warning, even if the compiler generates code to do the "right"
thing.

The purpose of include files is to keep you from having to re-enter
the same code over & over & over & over again. They also allow for
some runtime type checking.

   Don't bash something you don't know anything about. Manx is (and ever was)
   a fine product.

Must be - it generates some very fanatical defenders. I've twice seen
them trying to redefine the C langage to be what Manx compiled,
instead of vice versa.

	<mike
--
I went down to the hiring fair,				Mike Meyer
For to sell my labor.					mwm@relay.pa.dec.com
I noticed a maid in the very next row,			decwrl!mwm
I hoped she'd be my neighbor.