function returning pointer to itself

Dave Jones djones at megatest.UUCP
Thu Jul 21 09:54:05 AEST 1988


>From article <1065 at agora.UUCP>, by rickc at agora.UUCP (Rick Coates):
> In article <5485 at batcomputer.tn.cornell.edu>, olson at batcomputer.tn.cornell.edu (olson) writes:
>> Does the new C standard have a natural way to declare a
>> function that returns a pointer to itself
>> ????
>> 
> (deleted)
> (several more articles, with obscure declartions)
> 
> Please forgive me if I'm missing something obvious here.....
> (I used to be a hardware person - what can I say).
> 

  You are forgiven.  What can you say?  Well, let's see...
  Say ten _Hail_Richies_ and sin no more.

  There are two problems, really.  One is to declare the thing
  so that it will work on any complying implementation, perhaps
  by resorting to a type-cast.  There have assertions to the effect
  that void* is not castable to a function-pointer and vice versa.
  (Some wierd architectures use different formats for data pointers 
  and function pointers, which again proves that most hardware 
  designers should be in jail.   I'm sure _you_ never did anything like
  that when you were in the hardware game, right?)

  The second problem is actually to make a proper type-declaration --
  by that I mean one which does not require a type-cast.  That can't
  be done.  You can, however, return a structure which contains a
  field pointing to the function, as a couple of correspondants have
  demonstrated.


> I just tried this out on the only dpANSI compiler I have available -
> Microsoft C 5.1 for IBM pc's 

...

> This seems to work fine. Compiles with no errors or warnings.
> 
> Isn't this a reasonable use of void *   ???
> It isn't even Ugly!
> -------------program------------
> void * test_func();
> 
> main()
> {
>     printf("test: %x\n",test_func);
> }
> 
> void *
> test_func()
> {
>     return test_func;
> }
>


I think your compiler is broke.  It is my understanding that it
should warn you that the "return test_func" line does not return a void*.
Here's what the Sun3 compiler says:

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

If your compiler is this lax on pointer types, I would suspect that
it allows some obscure bugs to slip through without warning.


		later,
		Dave J.


P.S.

    I didn't really mean it about the hardware designers belonging in
jail.  I think that a comprehensive work-furlow program with adequate
counciling and group programming support sessions should be the norm
in a modern society.


		D.J.



More information about the Comp.std.c mailing list