typedefing functions (Re: Is typedef char BUFFER[20] legal?)

Erik Naggum enag at ifi.uio.no
Tue Feb 5 05:28:19 AEST 1991


In article <1991Feb4.053613.3758 at watmath.waterloo.edu>, David Tanguay writes:

> Our compiler had a neat bug with this. It would accept, e.g., 

> 	typedef int f( int a ) { return a+1; }

> "f" would be a type thereafter in that file, but it was also created
> as an external visible only outside that file.  Syntactically typedef
> is the same as static or extern.

> Is the above typedef valid in ANSI-C (as a typedef)?  It's not a
> valid function declaration (has to be static or extern), but I can't
> recall anything making it an invalid typedef.

No.  You cannot achieve function type through typedef.  I remember
this phrase from the standard, but can't find it right now.  Note that
you can say

	typedef int f (int a, char *b);

and later say

	f foo, bar, zot;

and

	f *functab[3] = { foo, bar, zot, };

The functions would be declared

	f foo
	{ ... }

	f bar
	{ ... }

	f zot
	{ ... }

This will, naturally, make programs like ctags somewhat less likely to
find the functions, but it's a very good way to provide consistent
interface to functions that are referenced only through function
pointers.

--
[Erik Naggum]					     <enag at ifi.uio.no>
Naggum Software, Oslo, Norway			   <erik at naggum.uu.no>



More information about the Comp.std.c mailing list