[comp.lang.c] C struct definition according to K & R.

robert@sri-spam.istc.sri.com (Robert Allen) (03/10/87)

	I have been programming in C for about 5 years, on DEC and Sun
	C compilers for UNIX.  Recently several persons stated a point
	which I find difficult to believe, however this point has
	also been brought up in. re. the ANSI C standard, and I would
	apprecitate some opinions from netters.

    According to "The Proposed ANSI C Language Standard, part 5" by
    Rex Jaeschke in _Programmers_World_, Jan-Feb '87, the following
    is true (I quote verbatim):

	"K&R (page 197) says 'Two structures may share a common initial
	 sequence of members; that is, the same member may appear in
	 two different structures if it has the same type in both and
	 all previous members are the same in both. (Actually, the
	 compiler chekcs only that a name in two different structures
	 has the same type and offset in both, but if preceding members
	 differ the construction is nonportable.)'

	 This meant that the following declarations generated a syntax
	 error since i and l had different types and offsets.

	    struct s1 {
		int i;
		long l;
	    }

	    struct s2 {
		long i;
		int l;
	    }
	"

    My understanding of the pertinent K&R text is, simply, the opposite.
    That you *may* have the above construct in C.  I believe that the issue
    was discussed in K&R for some unknown reason, possibly because a language
    in use at the time the C spec was written (?) *did not* allow such naming
    conventions.  In short, I believe that K&R were saying, "while constructs
    such as this (above) may cause problems in some languages, they don't in
    C."

    My question to the net is, do you believe that the above constructs are
    strictly legal in K&R C, or do you believe they are illegal in K&R C.
    I'm interested in the C aspect, not so much how a given compiler does
    it (although that would be much appreciated).  If you reply by mail or
    on the net, please include the # of years you've been using C, and which
    machines/compilers you use.  Thanks!
-- 
---------------------------------------------------------
    "Ohhhh, God hates me..."

    "Hate 'em back, it works for me!"

    "PUNTA, PUNTA, PUNTA!!"

Robert Allen,
robert@spam.istc.sri.com
---------------------------------------------------------

perry@vu-vlsi.UUCP (03/11/87)

In article <9929@sri-spam.istc.sri.com>
 robert@sri-spam.istc.sri.com (Robert Allen) writes:
> ..
>    In short, I believe that K&R were saying, "while constructs
>    such as this (above) may cause problems in some languages, they don't in
>    C."

   I agree with that, and so does our compiler (Pyramid 90x OSx3.1) after
adding some semicolons after the struct's { }

	    struct s1 {
		int i;
		long l;
	    };

	    struct s2 {
		long i;
		int l;
	    };

>     on the net, please include the # of years you've been using C, and which
>     machines/compilers you use.  Thanks!

  I've been using C for 0.000000E+00 years, no machines or compilers, I
just compile and execute it by inspection.

...Rick			..{cbmvax,pyrnj,bpa}!vu-vlsi!perry
			perry@vuvaxcom.bitnet

amos@instable.UUCP (03/11/87)

In article <9929@sri-spam.istc.sri.com> robert@sri-spam.istc.sri.com (Robert Allen) writes:
>    My question to the net is, do you believe that the above constructs are
>    strictly legal in K&R C, or do you believe they are illegal in K&R C.
>    I'm interested in the C aspect, not so much how a given compiler does
>    it (although that would be much appreciated).

The  language  described in  K&R  is  that  accepted  by the  PDP-11  v7
compiler, which is slightly different than that accepted by the now more
common pcc compiler.  The main difference is that  for copatibility with
the  v6 compiler,  which accepted  a more  primitive dialect  of C  that
didn't have  'union' and  'enum', all struct  members' names  are pooled
into a common  name space. That makes the given  example illegal in some
compilers, though accepted as legal by the pcc compiler.

Many  ambiguities in  K&R arise  from the  fact that  it was  written to
describe one dialect,  then, just before its  publication, was converted
to describe a slightly different dialect.

>    If you reply by mail or
>    on the net, please include the # of years you've been using C, and which
>    machines/compilers you use.  Thanks!

I have been  working in C since 1977  on the PDP-11 v6 and  v7, and have
worked with the pcc (portable) compiler on the VAX, CCI6/32 for (which I
did the adaption) and on NS32000 series micros.
-- 
	Amos Shapir
National Semiconductor (Israel)
6 Maskit st. P.O.B. 3007, Herzlia 46104, Israel
(011-972) 52-522261  amos%nsta@nsc.com 34.48'E 32.10'N