Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!necntc!ima!think!rst From: rst@think.COM (Robert Thau) Newsgroups: comp.lang.c Subject: Re: I'd like to . . . Message-ID: <11661@think.UUCP> Date: Wed, 11-Nov-87 16:29:29 EST Article-I.D.: think.11661 Posted: Wed Nov 11 16:29:29 1987 Date-Received: Sat, 14-Nov-87 03:00:43 EST References: <10224@brl-adm.ARPA> <6660@brl-smoke.ARPA> <511@interlan.UUCP> Sender: usenet@think.UUCP Reply-To: rst@mneme.UUCP (Robert Thau) Organization: Thinking Machines Corporation, Cambridge, MA Lines: 44 In article <511@interlan.UUCP> deem@interlan.UUCP (Mike Deem) writes: >I think this could be declared something like: > > struct { > int c; /* c takes up space */ > union { /* no identifier */ > int a; /* both a and b are in the same space and */ > float b; /* that space follows c's */ > }; /* no identified here either */ > char d; /* d takes space following the a b space */ > } s; "Fixed in C++", which supports exactly this syntax. In plain C, there is no way to suppress the name of the union entirely, so the typical thing is to hide it with a little preprocessor skullduggery. For example, struct s { int c; /* c takes up space */ union { int A; /* both a and b are in the same space and */ float B; /* that space follows c's */ } u; char d; /* d takes space following the a b space */ }; #define a u.A #define b u.B main() { struct s s; printf("&s.a = %d, &s.b = %d, &s.c = %d, &s.d=%d\n", (int)&s.a, (int)&s.b, (int)&s.c, (int)&s.d); } on a Sun-3 (where pointers fit in ints) produces the output: &s.a = 251657764, &s.b = 251657764, &s.c = 251657760, &s.d=251657768 which shows that a and b do in fact have the same address. rst