robinson (Jim Robinson) (07/26/90)
It is my understanding of K&R I C that a variable of type char * may only contain a pointer to a data object, and that assigning a pointer to a function to such a variable is not guaranteed to work since there is no requirement that pointers to objects be the same size as pointers to functions. 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? -- Jim Robinson {uunet,ubc-cs}!van-bc!mdivax1!robinson
chris@mimsy.umd.edu (Chris Torek) (07/26/90)
In article <1990Jul25.213257.7872@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@cs.umd.edu Path: uunet!mimsy!chris
steve@taumet.com (Stephen Clamage) (07/26/90)
In article <1990Jul25.213257.7872@mdivax1.uucp>: >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. ANSI C Standard section 3.2.2.3 says: "A pointer to void may be converted to or from a pointer to any incomplete or object type." An "object" is a data object, not a function. Function pointers are everywhere discussed separately. -- Steve Clamage, TauMetric Corp, steve@taumet.com