function returning pointer to itself

Dave Jones djones at megatest.UUCP
Mon Jul 18 11:42:39 AEST 1988


>From article <7797 at watdragon.waterloo.edu>, by smking at lion.waterloo.edu (Scott M. King):
> In article <664 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
>>From article <7725 at watdragon.waterloo.edu), by smking at lion.waterloo.edu (Scott M. King): (me)
>>) In article <5485 at batcomputer.tn.cornell.edu) olson at tcgould.tn.cornell.edu (olson) writes:
>>))Does the new C standard have a natural way to declare a
>>))function that returns a pointer to itself
>>)
>>) void *(*
>>) main() )()
>>) {
>>) 	return main;
>>) }
>>) 
>>) Ugly eh???
>>
>>Ugly? Yes. Correct?  No.  Don't feel too bad, though.
> 
> Oh, don't worry, I don't feel too bad. My declaration *is* correct.
>
  [Incorrect correctness argument omitted.]
>

At the risk of starting a "'Tis. 'Tain't" kind of argument, I must 
respectfully state "Tain't."

Here's what the Sun3 4.3 C compiler says, yanked dircectly with
the "put" and "get" keys:

"foo.c", line 4: warning: illegal pointer combination

Someone -- I'm regret that I have discarded the letter, and cannot
recall his name -- pointed out that you *can* declare a function that
returns a *structure* containing a pointer to the function, as follows:

typedef struct ugly {
  struct ugly (*pf)();
} fptUG;

fptUG
main()
{
  fptUG retval;
  retval.pf = main;
  return retval;
}


As I pointed out in the first response, the "struct" name-space
is the only one which allows for forward reference.

He said that the above solution was close enough for him.  Well, okay,
I'll go along with it, although I don't know what "struct ugly" has to
do with anything.



			-- Dave J.



More information about the Comp.std.c mailing list