void * and pointers to functions

Chris Torek chris at mimsy.umd.edu
Thu Jul 26 15:25:09 AEST 1990


In article <1990Jul25.213257.7872 at mdivax1.uucp> Jim Robinson writes:
>It is my understanding of K&R I C that a variable of type char * may only
>contain a pointer to a data object ...

Correct (among other possibilities is, e.g., the Univac compiler in
which object pointers are one or two words but function pointers are
nine words long).

>After reading the relevant parts of K&R II it would appear that a variable of
>type void * may legally contain a pointer to a function as well as a
>pointer to any of the various data types. Is this correct?

No; the old restriction continues to apply.

What *is* guaranteed in New (ANSI) C is that all function pointers can
be stored in all other function pointers.  That is, given an arbitrary
pair of types T1 and T2, both of which are some kind of `pointer to
function returning ...', one can legally write:

	T1 p1; T2 p2;
	...		(set p2 to point to some actual function)
	p1 = (T1)p2;
	...
	(*(T2 *)p1)(args);

Note the final cast back to T2 before the call---without this the compiler
is not obliged to generate correct code.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list