Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!purdue!haven!mimsy!chris From: chris@mimsy.umd.edu (Chris Torek) Newsgroups: comp.lang.c Subject: Re: union or type casting, which to use? Message-ID: <28280@mimsy.umd.edu> Date: 4 Dec 90 13:03:04 GMT References: <9011271554.AA23331@bisco.kodak.COM> <28080@mimsy.umd.edu> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 47 In article <28080@mimsy.umd.edu> I wrote: >I think that it is possible to conclude, given nothing more than >X3.159-1989 and `ANSI standard nomenclature', that all struct pointers >*are* the same size, but that it is *not* possible to conclude that >they have the same format (e.g., different types might use different >bits, where struct pointers to A use the low bits and struct pointers >to B use the high bits). ... > > struct i { int x; }; > struct c { char x; }; > union u { struct i *i; struct c *c; } u; > struct c foo; > > foo.x = 'a'; > u.i = (struct i *)&foo; > printf("u.c->x = %c\n", > u.c->x /* ERROR? */ > ); Tony Hansen sent me an interesting argument based on the idea of `incomplete structure pointers'. Given a module.h that declares: struct xyz; typedef struct xyz *hidden_t; hidden_t newxyz(void); void xyz(hidden_t), endxyz(hidden_t); #define NOXYZ ((hidden_t)0) and a separate module that uses it, it becomes clear that at least nil pointers to `xyz' structures must be known/knowable `outside' the module that actually defines them. With some stretching (and the use of `void *' intermediaries), the argument can be extended to non-nil pointers as well. There is, however, a counterargument that applies to both of these. There is no rule that says that the compiler must generate real code until it `has all the pieces'. `cc -c foo.c' might write a foo.o that contains an intermediate form, and only when foo.o is `linked' with module.o (which actually defines `struct xyz') are the pointers given final types, and only then would the compiler generate the machine instructions needed to use those pointers. This argument might even obviate the need to make all struct pointers the same `size'. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@cs.umd.edu Path: uunet!mimsy!chris Brought to you by Super Global Mega Corp .com