Topic: Declaring function parameters


Author: sdm@cs.brown.edu (Scott Meyers)
Date: 23 Aug 91 17:21:09 GMT
Raw View
Suppose you want to declare a function f taking a function parameter, and
that parameter itself takes a function as a parameter.  Don't ask why, just
suppose.  cfront 2.1 takes this without a quibble:

   void f( int pf1( int pf2(int) ) );  // okay

Since this is just a declaration of f, you should be able to omit the
parameter names, but cfront gets cranky when you try this:

   void f( int ( int (int) ) );        // "syntax error"

So you explicitly make the parameters pointers, though they're still
nameless.  cfront says okay:

   void f( int (*)( int (*)(int) ) );  // okay

Functions omitting a return type are implicitly of type int, so you try to
remove the return types, but cfront just says no:

   void f( (*)( (*)(int) ) );          // "syntax error"

Can you help me make sense of this?  When can the pointer part of a
pointer-to-function be left implicit?  When can the implicit return type of
a function be left implicit?  In short, why can't I say this?

   void f( ( (int) ) );                // should be same as 1st example

Scott


-------------------------------------------------------------------------------
What do you say to a convicted felon in Providence?  "Hello, Mr. Mayor."




Author: steve@taumet.com (Stephen D. Clamage)
Date: 24 Aug 91 21:19:09 GMT
Raw View
sdm@cs.brown.edu (Scott Meyers) writes:

>Suppose you want to declare a function f taking a function parameter, and
>that parameter itself takes a function as a parameter...
>cfront 2.1 takes this without a quibble:

>   void f( int pf1( int pf2(int) ) );  // okay

This is legal, since a formal function parameter is taken to be
a pointer to a function.

>Since this is just a declaration of f, you should be able to omit the
>parameter names, but cfront gets cranky when you try this:

>   void f( int ( int (int) ) );        // "syntax error"

You have done more than omit parameter names.  You have changed the
meaning.  For example,
 int()
is not of type "pointer to function with no params returning int".
Some sort of (abstract) declarator is needed to make a function or
pointer-to-function type, and there is none here.  It is a syntax error.
You have to do one of these:
 int (*)()
 int (*fp)()
 int fp()

>So you explicitly make the parameters pointers, though they're still
>nameless.  cfront says okay:

>   void f( int (*)( int (*)(int) ) );  // okay

Right, since you now have supplied abstract declarators to fill out the
syntax.

>Functions omitting a return type are implicitly of type int, so you try to
>remove the return types, but cfront just says no:

>   void f( (*)( (*)(int) ) );          // "syntax error"

You cannot omit the return type when declaring a pointer-to-function
in any context.
--

Steve Clamage, TauMetric Corp, steve@taumet.com