pcc compiler error

Rob Warnock rpw3 at redwood.UUCP
Tue Feb 12 11:03:26 AEST 1985


+---------------
| 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



More information about the Comp.lang.c mailing list