Recursive function declarations

Mark Brader msb at sq.uucp
Fri Jul 22 04:02:40 AEST 1988


> >[ 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 at sq.com	will fix them...	-- Karl Heuer



More information about the Comp.lang.c mailing list