[net.lang.c] structure problem - error or warning?

jim@ccice5.UUCP (James J. Roche) (10/12/84)

Why does the C compiler only give a warning instead of an error on the
use of an illegal structure member. The following code example demonstrates
what I mean:

main()
	{
	struct v {
		int w;
		};

	struct x {
		int y;
		} z;

	z.w = 0;
	}

The C compiler gives the warning "illegal member use: w" on the line
z.w = 0.  It seems to me that this should be an error. Is this a bug
in the C compiler? Thanks in advance for any answers that you can provide.
-- 

		Jim Roche
		Computer Consoles Inc.
		ccice5:jim (CCI Central Engineering systems only)
		{rochester, ritcv, ccivax, rayssd, rlgvax}!ccice5!jim (UUCP)

keesan@bbncca.ARPA (Morris Keesan) (10/13/84)

>Why does the C compiler only give a warning instead of an error on the
>use of an illegal structure member. The following code example demonstrates
>what I mean:
>
>main()
>	 {
>	 struct v {
>		 int w;
>		 };
>
>	 struct x {
>		 int y;
>		 } z;
>
>	 z.w = 0;
>	 }
>
>The C compiler gives the warning "illegal member use: w" on the line
>z.w = 0.  It seems to me that this should be an error. Is this a bug
>in the C compiler? Thanks in advance for any answers that you can provide.
>-- 
>
>		 Jim Roche
>		 Computer Consoles Inc.

Well, it depends on what you mean by "the C compiler".  It obviously boils down
to a question of which C compiler you're using, and what version of the
language it purports to compile.  Kernighan & Ritchie C explicitly allows this
member use, which is in no way illegal (Section 14.1 of the reference manual:
"In fact, any lvalue is allowed before ., and that lvalue is then assumed to
have the form of the structure of which the name on the right is a member.").

The System V Release 1 version of the language changes this behaviour, and
explicitly promises to give messages of the type you cite.  From the System V
"User Level Changes to the C Language" (in the "Transition Aids" document):
"Prior to the language change these references [of the 'z.w' type] were
considered legitimate.  However with the current release of PCC these will
produce non-fatal error messages."
-- 
			    Morris M. Keesan
			    {decvax,linus,ihnp4,wivax,wjh12,ima}!bbncca!keesan
			    keesan @ BBN-UNIX.ARPA

mark@tove.UUCP (Mark Weiser) (10/14/84)

> Why does the C compiler only give a warning instead of an error on the
> use of an illegal structure member. 

That's no bug, that's a feature!  It is a kind of cheap union, 
in which you can pick apart anything as though it were a structure.
Now that C has real unions it is unnecessary, but there is a lot of
C out there that depends on this "bug".
-- 
Spoken: Mark Weiser 	ARPA:	mark@maryland
CSNet:	mark@umcp-cs 	UUCP:	{seismo,allegra}!umcp-cs!mark
U.S.: Computer Science Dept., University of Maryland, College Park, MD 20742

martillo@mit-athena.ARPA (Joaquim Martillo) (10/15/84)

I  think  warning  for illegal member use rather than error dates to the
ancient history of the c compiler when there were  not  unions  and  you
used to able to get member elements from an object which was not defined
as a structure.  I seem to remember two different structures  could  not
have  the  same member.  Some system code was written and depends on the
ability to get members from objects not defined as structures, hence the
warning.

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (10/16/84)

The reason your compiler gives a nonfatal warning on misuse of
structure members (z.w where w is a member of a different structure
type) is that in the early days of C, this was a supported usage.
Now that stronger typing is creeping into C, it really should be
considered an error (if one WANTS that behavior he can get it by
appropriate use of type-casts).  "Compatibility forever"

ron@brl-tgr.ARPA (Ron Natalie <ron>) (10/17/84)

> Why does the C compiler only give a warning instead of an error on the
> use of an illegal structure member. The following code example demonstrates
> what I mean:
> 
You mean "Why does MY C compiler," not THE C compiler.  It would be
nice if you mentioned which compiler you were talking about.

-Ron