Function-types: compatability, and typedefs
Doug Gwyn
gwyn at smoke.BRL.MIL
Thu Sep 7 13:59:25 AEST 1989
In article <660059 at hpclwjm.HP.COM> walter at hpclwjm.HP.COM (Walter Murray) writes:
>>> extern int example( MonadicStringFunction arg );
>>>Would I need to add the * to ensure the argument is treated as a function
>>>pointer...
>>I think so. You want a pointer to a function, you should declare it as such.
>>The automatic conversion of identifier designating a function to a pointer
>>occurs only in expressions, not in declarations.
>As I read the dpANS, the * is not necessary. There is an adjustment that
>occurs in function declarations and definitions, analogous to the
>automatic conversion that occurs in expressions. See 3.5.4.3 and 3.7.1.
>(In the dpANS of 12/7/88, see page 69, lines 22-24, and page 83,
>lines 23-26.)
It's true that in 3.7.1 it's made clear that when a function is called
with an argument of function type, the argument is converted to pointer
to function. That makes Walter's interpretation a consistent one. All
that 3.5.4.3 covers is when two function types are compatible, which
doesn't so far as I can see cover the question. However, I'm willing
to take 3.7.1 as providing a sufficient "as if" constraint to make what
I would consider the "wrong" declaration completely interchangeable
with the "right" one.
Even if the * is not necessary, it is allowed and I think more clearly
documents the interface.
>>>Can I also use the typedef in a *definition*:
>>> MonadicStringFunction boring( char *arg ) { return arg; }
>>With the first (non-pointer) typedef, you're supposed to be able to.
>I don't think so, if I am understanding the question right. It is
>forbidden by a constraint in 3.7.1. See footnote 76 on page 82.
I agree with this; I missed the constraint Walter mentions.
The key is that in the syntax
function_definition ::= declaration_specifiers{opt} declarator
declaration_list{opt} compound_statement
the declaration_specifiers are entirely associated with the function
return type, so the above example would be an attempt to declare a
function that returns a MonadicStringFunction, which is of course illegal.
The "declarator" is basically constrained to include one identifier (the
name of the function), a bunch of parentheses, possibly parameter list,
*s, etc. but no place to introduce the desired typedef. The optional
declaration_list is for old-style parameter declarations. Thus there's
no place that you could use a typedef for the actual function type. You
could use one for the function return type (only).
More information about the Comp.lang.c
mailing list