[net.lang.c] pcc compiler error

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

jeff@aids-unix (Jeff Dean) (02/23/85)

> From: Kevin Szabo <wateng!ksbszabo>
> 
> >	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.
> 

It seems to me that the code is legal C (though not necessarily
all that meaningful).  Since C now allows structures to be passed
as parameters, it appears inconsistent to allow something like
"foo(*p)" but not "if(*p)".

Since the code is legal, lint has no reason to complain.  The
error message you received indicated that the back end of your
compiler was not able to generate code.  There are C compilers
that will compile this program.

	jd

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (02/23/85)

> > >		struct xy *p;
> > >	 	if (*p) {
> > >			/* compiler error: zzzcode- bad type */
>
> Since the code is legal, lint has no reason to complain.  The
> error message you received indicated that the back end of your
> compiler was not able to generate code.  There are C compilers
> that will compile this program.

As previously posted, this is NOT legal C.  What does it mean to
test a struct for zeroness?  One could presumably add something to
C for this case but it seems pretty bogus to me.

henry@utzoo.UUCP (Henry Spencer) (02/27/85)

> It seems to me that the code is legal C (though not necessarily
> all that meaningful).  Since C now allows structures to be passed
> as parameters, it appears inconsistent to allow something like
> "foo(*p)" but not "if(*p)".
> 
> Since the code is legal, lint has no reason to complain.  The
> error message you received indicated that the back end of your
> compiler was not able to generate code.  There are C compilers
> that will compile this program.

Sorry, the code is not legal.  Passing a struct as a parameter is
basically struct assignment, which is in modern C.  Giving a struct
to an if is struct comparison (against 0), which is not and has never
been part of C.  (It's not in the current ANSI C draft either.)
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry

annab@azure.UUCP (A Beaver) (03/01/85)

>References: <8494@brl-tgr.ARPA>

> From: Kevin Szabo <wateng!ksbszabo>
> 
> >	struct xy  {
> >	 	int x;
> >	 	int y;
> >	 } a;
         ^^^
In looking through K&R, on page 123, the example which is shown,
shows that 'a' should be '*a'. Maybe THATS what it's complaining
about?
> >	 main()
> >	 {
> >		struct xy *p;
> >		p = &a;
> >	 	if (*p) {
> >			/* compiler error: zzzcode- bad type */
> >		}
> >	}

	 Annadiana Beaver
	A Beaver@Tektronix	" You think the present is moving fast,
				  wait 'til you see the future." -Ruby-