Ptr to func doesn't like casting??

Henry Spencer henry at utzoo.UUCP
Wed Dec 12 06:23:42 AEST 1984


> I always had the impression K&R didn't make an exception for function
> pointers when it stated that different pointer types can be cast into
> each other. Oh well.

K&R didn't, but then we all know that casts were a pretty late addition
to C and K&R didn't really describe them very well at all.

> What does the ANSI committee suggest for a generic function pointer? Or
> does (void *) still work?

They don't have any specific suggestion that I've seen.  "void *" is the
generic pointer-to-object, but functions are not objects.

> Using a union is not always a practical solution. ...

Agreed.  Note that you *can* cast pointers to functions around "among
themselves", so to speak; it's just that you can't mix them with pointers
to objects without trouble.  So you need to start thinking about unions
only in really unusual situations.  The example you give (passing a
generic "pointer to function") isn't a problem here, although it would
be nice if there were a function-pointer equivalent of "void *".

> Are there machines which cannot cast a function pointer into another
> pointer (e.g. (void *)) and back? After all, you can cast any pointer
> into an implementation-dependant integral type and back without loss
> of information...

I'm not aware of any... but they may exist.  Casting pointers to the
proper integral type and back is always workable; perhaps this is the
thing to do in dire extremity.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry



More information about the Comp.lang.c mailing list