Xref: utzoo comp.lang.c:26758 comp.lang.misc:4395 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!mcsun!ukc!cam-cl!nmm From: nmm@cl.cam.ac.uk (Nick Maclaren) Newsgroups: comp.lang.c,comp.lang.misc Subject: Re: C strongly typed? Message-ID: <1798@gannet.cl.cam.ac.uk> Date: 8 Mar 90 12:35:32 GMT References: <259@eiffel.UUCP> <1990Mar1.172526.28683@utzoo.uucp> <849@enea.se> <1990Mar7.182230.5517@utzoo.uucp> Sender: news@cl.cam.ac.uk Reply-To: nmm@cl.cam.ac.uk (Nick Maclaren) Organization: U of Cambridge Comp Lab, UK Lines: 35 Henry Spencer writes: > .... C's type system is not extensible unless > you count "struct", but the language is strongly typed -- mixing random > types is not allowed. I am afraid that I must disagree with this. While mixing RANDOM types is not allowed, even ANSI C permits a bewildering variety of type punning. For example: double x, y; memcpy(&x,&y,sizeof(double)); The ANSI standard (and K&R) explicitly require that any data type may be treated as an array of characters (under certain circumstances, such as the above). A huge proportion of the library relies upon this to work at all (e.g. much of string.h, some of stdlib.h, some of stdio.h). union {void *a; char *b;} fred; fred.a = ...; ... = fred.b; This example is a curiosity in ANSI C: while it is illegal, and the compiler is entitled to throw it out, it is also required to work! The reason is that 'char *' and 'void *' are different types but are required to have the same representation and alignment. There are a large number of more obscure cases, many of which are relied upon by traditional C programs. Good, portable ones avoid such constructions if at all possible, but sometimes their use is essential. Nick Maclaren University of Cambridge Computer Laboratory nmm@cl.cam.ac.uk