C pet peeve - (nf)

tektronix!tekecs!randals tektronix!tekecs!randals
Sat Mar 26 17:54:42 AEST 1983


In a recent article from:

	Bill Lee
	lee at utexas-11
	...!ucbvax!nbires!ut-ngp!lee
	...!eagle!ut-ngp!lee

he indicates that the way to get unions without the additional
level of indirection is to do something like:

	#define as_int o.as_int1;
	#define as_ptr o.as_ptr1;

	struct foo {
		short type;
		union {
			int as_int1;
			char *as_ptr1;
		} o;
	} bar;

DON'T APPEND the semicolon to the definition!!  If you refer
to bar.as_int, you will get (expanded) "bar.o.as_int1;", which
will undoubtedly botch the compile (an extra semicolon).

Remember, #define's are *not* "C" code... they are simply
straight substitutions!  Make them look like this:

	#define as_int o.as_int1
	#define as_ptr o.as_ptr1

Randal L. ("sometimes below C-level") Schwartz
Tektronix Engineering Computing Systems (the UNIX folks)
Wilsonville, Oregon, USA

UUCP:	...!XXX!teklabs!tekecs!randals (ignore return address)
	(where XXX is one of: aat cbosg chico decvax harpo ihnss
	lbl-unix ogcvax pur-ee reed ssc-vax ucbvax zehntel)
CSNET:	tekecs!randals @ tektronix
ARPA:	tekecs!randals.tektronix @ rand-relay



More information about the Comp.lang.c mailing list