functions returning pointers to functions

Gary Samuelson garys at bunkerb.UUCP
Thu Dec 15 06:17:19 AEST 1983


In reply to Steve Summit, who wanted a function returning a
pointer to a function returning an integer:

I tried to write this reply in the same style as your request,
but I guess I don't speak King James English well enough.  Will
Today's English suffice :-) ?

Using a typedef divides the declaration into bite-sized pieces,
which the compiler can swallow.

The following program, which I have compiled and run on our VAX
(4.1BSD), contains such a function.  Simply tear along the dotted
line.

Gary Samuelson
decvax!ittvax!bunker!bunkerb!garys
-------------------------------------------------
/*
 * a couple of functions which return integers
 */

int g( x )
int x;
{
	return( x + x );
}

int h( x )
int x;
{
	return( x * x );
}

/*
 * A function which returns a pointer to a function
 * which returns an integer.
 */

typedef int (*ifp)() ;
ifp f( a )
int a;
{
if( a == 0 )
	return( g );
else
	return( h );
}

main()
{
	printf( "%d\n", ((*f)( 0 )) ( 4 ) );
	printf( "%d\n", ((*f)( 1 )) ( 4 ) );
}



More information about the Comp.lang.c mailing list