Checksum: 11793
Lines: 45
Path: utzoo!sq!msb
From: msb@sq.uucp (Mark Brader)
Date: Thu, 21-Jul-88 14:02:40 EDT
Message-ID: <1988Jul21.140240.12516@sq.uucp>
Newsgroups: comp.lang.c
Subject: Re: Recursive function declarations
Summary: wrap in struct; nothing else works
References: <323@sdrc.UUCP> <5298@june.cs.washington.edu>
Reply-To: msb@sq.com (Mark Brader)
Organization: SoftQuad Inc., Toronto

> >[ 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
