Help, page 197 K&R !!!

Steve Summit scs at adam.pika.mit.edu
Tue Aug 8 15:11:04 AEST 1989


In article <648 at kl-cs.UUCP> pc at cs.keele.ac.uk (Phil Cornes) writes:
>K&R C says that member names in structures and unions must (in general) be
>unique. This is because in early implementations the names of structure and
>union members were not associated with their parent names but were only stored
>as a type and offset from the start of the parent.

In article <18350 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>This is correct.  These early implementations had all but vanished by
>the time K&R 1st ed. started selling...

Not really; the pdp11's I and everyone I knew were learning C and
Unix on all had the single struct member namespace.  As far as I
know, the pdp11 compilers still do (unless someone has gotten
energetic in 2.10bsd or the like).

In article <10481 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <CLINE.89Jul1102015 at sun.soe.clarkson.edu> cline at sun.soe.clarkson.edu (Marshall Cline) writes:
>>	{	char	*p;
>>		p->squiggle = 3;  /* implicit cast of "p" to "(worm_t *)p" */
>
>I don't think that was ever allowed; some older C compilers did permit
>pointers to one structure type to be used to access members of another
>structure type, but that's as weird as I recall it getting.

Doug probably hasn't had the pleasure of using a pdp11 in a
while.  Chris is correct when he describes

>[the] side effect[s]
>of never looking at both the left and right sides of the `->' operator
>at the same time.  The compiler treated `->' as
>
>	fetch offset via rhs value
>	add offset to lhs value
>	load word (size determined by rhs type) from pointer result
>
>and, since all pointers were the same size as an int---after all, the
>language only ran on PDP-11s, not IBM PCs with mixed models or the
>like---the only requirement was that the lhs not be floating point.
>(`long' did not exist then.)

I just booted up my pdp11 (whose compiler does have long ints, by
the way) and typed in the following odious little program:

	struct {int i; double d;} x;
	
	main()
	{
	int i; char *p;
	i = p = &x;
	i->i = 2; p->d = 3.4;
	printf("%d %g\n", x.i, x.d);
	}

It compiles and runs, without warnings or errors, printing "2 3.4",
as expected.  Interestingly enough, pcc (4.2 or so bsd) also
compiles it, with four warnings, but it still runs.  A lot of old
code had this cavalier attitude towards structure pointers, so
much so that pcc goes to rather heroic backwards-compatible
efforts to support it.

It has been asserted that a declaration like

	struct (int i; double d;};

is patently useless (ANSI C may even disallow it) yet one day
while browsing through the infamous "Lions book" I came across
several such structure "declarations" in the old V6 source code,
along with one of John Lions' inimitable charitable comments
explaining what "the programmer" was trying to do/getting away
with.  It didn't matter that neither a structure tag nor
variables of that type were declared, as long as the member types
and offsets got entered in the structure member symbol table,
since char pointers, int pointers, and ints (often simply
defaulted as formal parameters) were all used fairly
interchangeably back then.

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list