Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site redwood.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!seismo!hao!hplabs!hpda!fortune!redwood!rpw3 From: rpw3@redwood.UUCP (Rob Warnock) Newsgroups: net.lang.c,net.bugs.4bsd Subject: Re: pcc compiler error Message-ID: <158@redwood.UUCP> Date: Mon, 11-Feb-85 20:03:26 EST Article-I.D.: redwood.158 Posted: Mon Feb 11 20:03:26 1985 Date-Received: Thu, 14-Feb-85 02:21:05 EST References: <814@enea.UUCP> Organization: [Consultant], Foster City, CA Lines: 48 Xref: watmath net.lang.c:4370 net.bugs.4bsd:1382 +--------------- | The following code tests the contents of a pointer to a struct and | makes pcc generate a "compiler error" message. Is it just bad programming | style or a compiler bug? -- Bjorn Eriksen ENEA DATA Sweden | | struct xy { int x; int y; } a; | main() { | struct xy *p; | p = &a; | if (*p) { | /* compiler error: zzzcode- bad type */ | } | } +--------------- It's neither a compiler bug nor "bad style", but purely a syntax error. Your code does NOT test "the contents of a pointer to a struct" as you claim; it tests the struct ITSELF! In C, the contents of "p" is denoted by just "p"; "*p" is the object pointer to by "p". You can get identically the same error from PCC by writing: struct xy { int x; int y; } a; ... if (a) ... /* same as "if (*p) ..." This is equivalent to "if (a != 0) ..." which is a double type error: first, C has no rule for coercing "0" to a struct; second, C doesn't have structure comparison. (At least your compiler says "Bad type"; the one I use says "Compiler loop, please simplify expression". ;-} ) If you want to test "p" for being a non-nil pointer, use: if (p) ... /* same as "if(p != 0)", or "if ( p != (xy *)0 )" */ If you want at least one of the components of the struct to be "true" (non-zero) use: if (p->x || p->y) ... /* same as "if( (*p).x || (*p).y )" */ Hope this clarifies things for you. Rob Warnock Systems Architecture Consultant UUCP: {ihnp4,ucbvax!dual}!fortune!redwood!rpw3 DDD: (415)572-2607 USPS: 510 Trinidad Lane, Foster City, CA 94404