typedefs and prototypes

Joe English english at panarea.usc.edu
Fri Sep 23 12:18:06 AEST 1988


In article <13664 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>Note that you cannot use a typedef to define a function type: i.e.,
>`typedef func_arg_int_ret_int int(int);' is illegal.  The problem is
>that, given this definition, there is nowhere to put the parentheses
>and the formal, so that transforming
>
>	/* identity fn */
>	int identity(int i) { return i; }
>
>to
>
>	func_arg_int_ret_int identity { return i??? }
>
>leaves us nowhere to declare `i'.  (If we needed no arguments, it
>might work, but it makes the grammar inordinately difficult.)

I found out by accident that such a construct does work in Borland's
Turbo C (tm):  I had a slew of functions that took the same parameters
to go into an array, and I included a prototype in the typedef to enable
type-checking when they were called indirectly.  It turns out that if
the typedef includes parameter names, like

typedef int func_t(int i, int j);

then function definitions will only work like this:

func_t foo {
  return i+j;
}

!!! Of course, func_t foo(int a, int b) is a "function returning a
function," so the omission of the parameters in the definition makes
'sense.'  I just tried this construct on gcc, and it (quite rightly)
doesn't work at all.  Question: is giving parameter names in a
function declaration (void foo(int i,int j);) part of the standard?
The Turbo manual says to do this for clarity, but I will drop this
practice if it is nonportable.

      /|/| Joe English
-----< | | j.e. 
  O   \|\| ARPA: english%lipari at oberon.usc.edu



More information about the Comp.lang.c mailing list