ber@enea.UUCP (Bjorn Eriksen) (02/10/85)
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? struct xy { int x; int y; } a; main() { struct xy *p; p = &a; if (*p) { /* compiler error: zzzcode- bad type */ } } -- Bjorn Eriksen ENEA DATA Sweden UUCP: {decvax,philabs}!mcvax!enea!ber ARPA: decvax!mcvax!enea!ber@berkeley.arpa mcvax!enea!ber@seismo.arpa
mjs@eagle.UUCP (M.J.Shannon) (02/10/85)
> The following code tests the contents of a pointer to a struct ... > > struct xy { > int x; > int y; > } a; > > main() > { > struct xy *p; > > p = &a; > if (*p) { > /* compiler error: zzzcode- bad type */ > } > } This is not legitimate code (and the compiler should point this out). The value of `*p' here is identical to the value of `a', a `struct xy'. Thus, another equivalent way of saying what you did (equally wrong, at least) is: `if (a != 0) { /* stuff */ }'. It just isn't valid to compare a structure value against an integer value. Perhaps what you meant to say is: `if (p != (struct xy *) 0) { /* stuff */ }'. -- Marty Shannon UUCP: ihnp4!eagle!mjs Phone: +1 201 522 6063
ksbszabo@wateng.UUCP (Kevin Szabo) (02/11/85)
> struct xy { > int x; > int y; > } a; > main() > { > struct xy *p; > p = &a; > if (*p) { > /* compiler error: zzzcode- bad type */ > } > } >This is not legitimate code (and the compiler should point this out). Our BSD 4.2 compiler generates the same error when I just compile it. As Marty Shannon says, the code is incorrect. The thing that mystifies me is that LINT didn't even peep when I sent the code to it. Thus whatever is wrong with CC is also confusing lint, but in a way that causes LINT to incorrectly shut up. Kevin -- Kevin Szabo watmath!wateng!ksbszabo (U of Waterloo VLSI Group, Waterloo Ont.)
rpw3@redwood.UUCP (Rob Warnock) (02/12/85)
+--------------- | 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