how has C bitten you?

Peter Ludemann ludemann at ubc-cs.UUCP
Mon Aug 19 05:20:24 AEST 1985


Here's my favourite bite in the neck (apologies if I've made
any typos - this is just an example):

typedef union {
		int  u1;
		char u2;
	} union_type;

typedef struct {
		int        f1;
		union_type f2;
	} struct_type;

struct_type s;

s.u1 = 0;   /* should be: s.f1.u1 = 0 */

This has the effect of "s.f1 = 0" with no complaint from
the compiler (lint, of course, is another matter).  Truly
spectacular results can occur if "f1" is a pointer to
another area.

The really annoying thing is that K&R (page 186) says:
    A primary expression followed by a dot followed by an
    identifier is an expression.  The first expression must
    be an lvalue naming a structure or union, and the 
    identifier must name a member of the structure or union.
In other words, type checking almost as strong as Pascal's (yes,
I know about the case where two structures have the first fields
declared the same).

However, K&R (page 209) says "... this restriction is not
firmly enforced by the compiler."  It is sad that the defects
of the original C compiler have been slavishly copied by
subsequent ims.  If backward compatibility were
important a "don't check structures strictly" switch could
have been added to the compilers.
-- 
ludemann%ubc-vision at ubc-cs.uucp (ubc-cs!ludemann at ubc-vision.uucp)
ludemann at cs.ubc.cdn
ludemann at ubc-cs.csnet
Peter_Ludemann at UBC.mailnet



More information about the Comp.lang.c mailing list