struct/union/pointer query- A Summary

Ron Widell ron at motmpl.UUCP
Sat Jul 22 06:33:25 AEST 1989


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 at 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 at 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 at 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 at cbnewsc.ATT.COM (dennis.f.meyer)
crackle!tim (Tim Olson)
"Jeremy A. Siegel" <bridge2.ESD.3Com.COM!jzs>
"Stuart R. Kemp" <cs.umn.edu!kemp>
math.rutgers.edu!bumby (Richard Bumby)
cse.ogc.edu!schaefer (Barton E. Schaefer)
Walter Murray <hpda!walter>
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?



More information about the Comp.lang.c mailing list