[comp.lang.c] Recursive function declarations

scjones@sdrc.UUCP (Larry Jones) (07/15/88)

I am building a state machine program which is composed of a number of
functions.  Each function does it's thing and returns a pointer to the
next function to be called.  Each of the functions has exactly the same
type including return type and arguments.  My problem is exactly how to
declare these functions -- since the return type is identical to the
type of the function, you get an infinite recursion (foo is a function
returning a pointer to a function returning a pointer to a ...).

Anyone have any suggestions?

----
Larry Jones                         UUCP: ...!sdrc!scjones
SDRC                                AT&T: (513) 576-2070
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150
Nancy Reagan on superconductivity: "Just say mho."

pardo@june.cs.washington.edu (David Keppel) (07/16/88)

In article <323@sdrc.UUCP> scjones@sdrc.UUCP (Larry Jones) writes:
>[ Function returning pointer to function returning pointer to fun...]
>[ How do I declare it? ]

To the best of my knowledge (correct me, please), you can't  You
need to declare it as returning a most-general pointer (void* if you
have one) and then cast it when you call:


    #include <stdio.h>

    /* a function returning a void* cast pointer to a function */
	void*
    foo()
    {
	static int i = 10;
	return  i-- ? (void*)foo : NULL;
    }

    main()
    {
	typedef void *(*fptr)();	/* ptr to func ret ptr to void */
	fptr func = foo;

	while ( func = (fptr)(*func)() ) {	/*VALUSED*/
	    printf( "looping\n" );
	}
	exit(0);
    }

(Gcc compiles and runs this on a VAX.  Whether it's correct, I dunno.)

	;-D on  ( Better chemistry through living )  Pardo

msb@sq.uucp (Mark Brader) (07/21/88)

> >[ Function returning pointer to function returning pointer to fun...]
> >[ How do I declare it? ]

This question has just been asked, discussed, and resolved in comp.std.c.

> To the best of my knowledge (correct me, please), you can't.

He's right.

> You need to declare it as returning a most-general pointer (void* if you
> have one) and then cast it when you call...

He's wrong.

That trick doesn't work because function pointers can't necessarily be
fitted into object pointers.  void * (if provided, else char *) is only
the most general OBJECT pointer type.  Apparently there exists an
implementation where function pointers are several times larger than
object pointers.

The trick that does work is the same one you'd use for declaring a function
that you'd like to return an array:  wrap the return value in a struct.
This works because structs have a forward-reference syntax.  So you say:

	struct pf {
		struct pf (*pf)();
	};

	struct pf function()
	{
		struct pf tmp;
		...
		tmp.pf = function;
		return tmp;
	}

And invoke the thing thus:

	struct pf x;
	....
	x = function();
	(*x.pf)();		/* or just x.pf(); on modern compilers */

Mark Brader, Toronto		sed -e "s;??\\([-=(/)'<!>]\\);?\\\\?\\1;g"
utzoo!sq!msb, msb@sq.com	will fix them...	-- Karl Heuer

bill@proxftl.UUCP (T. William Wells) (07/23/88)

In article <1988Jul21.140240.12516@sq.uucp> msb@sq.com (Mark Brader) writes:
: > You need to declare it as returning a most-general pointer (void* if you
: > have one) and then cast it when you call...
:
: He's wrong.
:
: That trick doesn't work because function pointers can't necessarily be
: fitted into object pointers.  void * (if provided, else char *) is only
: the most general OBJECT pointer type.  Apparently there exists an
: implementation where function pointers are several times larger than
: object pointers.

Yes, but you can also use a function pointer, any function
pointer, as a generic function pointer; at least as far as ANSI
is concerned.