Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!gatech!mcdchg!motmpl!ron From: ron@motmpl.UUCP (Ron Widell) Newsgroups: comp.lang.c Subject: struct/union/pointer query- A Summary Message-ID: <1309@motmpl.UUCP> Date: 21 Jul 89 20:33:25 GMT Reply-To: ron@motmpl.UUCP (Ron Widell) Organization: Motorola Semiconductor, Minneapolis, MN. Lines: 71 My thanks to one and all who responded to my previous query re. structures in unions, and for patiently pointing out what should have been obvious (I used to think that I had a keen eye for the obvious :-)). In article <1298@motmpl.UUCP> I wrote that I couldn't understand why this wouldn't work: > main() > { > union { > unsigned short foo; > #ifdef BURP > struct burp { > #else > struct { > #endif > unsigned char fuz; > unsigned char bar; > } baz, *sptr; > } glorp, *uptr; > #ifdef BURP > struct burp baz, *sptr; > #endif > uptr = &glorp; > sptr = &glorp.baz; ^^^^ The answer, to quote tim@crackle (Tim Olson): |Without BURP defined, sptr is declared as an element of the union glorp |(just like baz) -- it is not a "top-level" structure definition. |Therefore, you must access it like: | | glorp.sptr = &glorp.baz; or, as others mentioned (after uptr has been assigned): uptr->sptr = &glorp.baz; So, when I wrote: > BTW #2- What I *think* I'm defining is a union with three members: 1) an > unsigned short (on the target machine it's an int). 2) a structure of 2 > unsigned chars. 3) a pointer to a structure of 2 unsigned chars. schaefer@cse.ogc.edu (Bart Schaefer) pointed out: |That's exactly what you did, but that isn't how you used it. Once again my profound thanks to the following people: arnor!freimer (Robert Freimer) dfmeyer@cbnewsc.ATT.COM (dennis.f.meyer) crackle!tim (Tim Olson) "Jeremy A. Siegel" "Stuart R. Kemp" math.rutgers.edu!bumby (Richard Bumby) cse.ogc.edu!schaefer (Barton E. Schaefer) Walter Murray arnor!uri (Uri Blumenthal) nluug.nl!nlgvax!smak!suhas SSD.HARRIS.COM!shirono (Roberto Shironoshita) Thanks, as well for being gentle with me (you must have known I'm a hardware type who's trying to learn). Regards, -- Ron Widell, Field Applications Eng. |UUCP: {...}mcdchg!motmpl!ron Motorola Semiconductor Products, Inc., |Voice:(612)941-6800 9600 W. 76th St., Suite G | I'm from Silicon Tundra, Eden Prairie, Mn. 55344 -3718 | what could I know?