[comp.sys.sgi] comparing pointers to void functions

cycy@isl1.ri.cmu.edu (Scum) (05/30/90)

Okay, this is a better tested question. I'm having a problem trying to
determine if a pointer is pointing to a certain function. The pointer is
defined as an element in a structure thusly:

struct object {
	...
	void (*formstart) ();
	...
}

As you can see, it is a pointer to a function which returns void. Now, I can
successfully invoke the function as:

struct object *thing;
...
	(thing->formstart)();

However, the compiler will not accept the following attempt to compare the
pointer to the address of the function, even though when they are printed
out, their values are the same:

	if (thing->formstart == bgnpolygon)

The compiler says "operands of == have incompatible types". This doesn't seem
right to me. Can anyone out there help me.... Please?


				-- Chris.
-- 

                                       -- Chris. (cycy@isl1.ri.cmu.edu)
"People make me pro-nuclear." -- Margarette Smith

rpw3@rigden.wpd.sgi.com (Rob Warnock) (05/30/90)

In article <9454@pt.cs.cmu.edu> cycy@isl1.ri.cmu.edu (Scum) writes:
+---------------
| Okay, this is a better tested question. I'm having a problem trying to
| determine if a pointer is pointing to a certain function. The pointer is
| defined as an element in a structure thusly:
| 	struct object {
| 		void (*formstart) ();
| 	}
| As you can see, it is a pointer to a function which returns void. Now, I can
| successfully invoke the function as:
| 	struct object *thing;
| 		(thing->formstart)();
| However, the compiler will not accept the following attempt to compare the
| pointer to the address of the function, even though when they are printed
| out, their values are the same:
| 		if (thing->formstart == bgnpolygon)
| The compiler says "operands of == have incompatible types". This doesn't seem
| right to me. Can anyone out there help me.... Please?
+---------------

Well, I originally thought it was because you needed to dereference the
function pointer in order to compare (like you "theoretically" should
have had to do in the call, but most compilers don't complain). That is:

 		(*thing->formstart)();
and
 		if (*thing->formstart == bgnpolygon)

But on the 3.2 C compiler that still gives the error you saw.

On a 3.3 (to be released) compiler, neither version of either form gives
an error (and the generated code looks correct). So I guess it's "fixed
in some to-be-determined release"...

Maybe some compiler-type person can help you with a workaround. Or you
might just try casting both sides of the compare to a (char *). [*Hack*]


-Rob

-----
Rob Warnock, MS-9U/510		rpw3@sgi.com		rpw3@pei.com
Silicon Graphics, Inc.		(415)335-1673		Protocol Engines, Inc.
2011 N. Shoreline Blvd.
Mountain View, CA  94039-7311

bron@bronze.wpd.sgi.com (Bron Campbell Nelson) (05/30/90)

In article <61180@sgi.sgi.com>, rpw3@rigden.wpd.sgi.com (Rob Warnock) writes:
> In article <9454@pt.cs.cmu.edu> cycy@isl1.ri.cmu.edu (Scum) writes:
[edited to reduced bandwidth]
> +---------------
> | ...                                     I'm having a problem trying to
> | determine if a pointer is pointing to a certain function. The pointer is
> | defined as an element in a structure thusly:
> | 	struct object {
> | 		void (*formstart) ();
> | 	}
> | However, the compiler will not accept the following attempt to compare the
> | pointer to the address of the function, even though when they are printed
> | out, their values are the same:
> | 		if (thing->formstart == bgnpolygon)
> +---------------
> ... on the 3.2 C compiler that still gives the error you saw.
> 
> On a 3.3 (to be released) compiler, neither version of either form gives
> an error (and the generated code looks correct). So I guess it's "fixed
> in some to-be-determined release"...

The short answer is that the compilers used in the SGI 3.2 release
had various sorts of problems with void and void* types.  This is one
of them.  As Rob says, these problems are fixed in the 3.3 version, due
out Real Soon Now.   Casting the types to char*() to do the compare is a
bit ugly, but I think it should work until 3.3 is available.

--
Bron Campbell Nelson
bron@sgi.com  or possibly  ..!ames!sgi!bron
These statements are my own, not those of Silicon Graphics.

moss@BRL.MIL ("Gary S. Moss", VLD/VMB) (05/30/90)

< 	if (thing->formstart == bgnpolygon)
< The compiler says "operands of == have incompatible types".

Chris,
	I ran into this exact problem yesterday.  It looks like a compiler
bug; I tried just about everything comparing a pointer to a function of
type void to the function name with the same result.  Casting each side
to a generic pointer type shuts up the compiler, but this is a non-portable
workaround since there is no guarantee in ANSI C that a function pointer
will fit in a generic *object* pointer type:

#ifdef mips
	if( (void *) thing->formstart == (void *) bgnpolygon )
#endif

	Anyone at SGI want to wedge  a fix in to the next release?

davea@quasar.wpd.sgi.com (David B. Anderson) (05/30/90)

In article <9005301007.aa09801@VMB.BRL.MIL> you write:
>< 	if (thing->formstart == bgnpolygon)
>< The compiler says "operands of == have incompatible types".
[stuff deleted]
>#ifdef mips
>	if( (void *) thing->formstart == (void *) bgnpolygon )
>#endif
>
>	Anyone at SGI want to wedge  a fix in to the next release?

I already did for the 3.3 release.  void  and void * are fully supported.

Note that using void * in 3.2 can result in 
	``Internal error: schain botch'' :-(

Regards,
[ David B. Anderson  Silicon Graphics  (415)335-1548  davea@sgi.com ]
[``What can go wrong?''                          --Calvin and Hobbes]