Help, page 197 K&R !!!

Marshall Cline cline at sun.soe.clarkson.edu
Sun Jul 2 00:20:36 AEST 1989


In article <648 at kl-cs.UUCP> pc at cs.keele.ac.uk (Phil Cornes) writes:

>From article <646 at kl-cs.UUCP>, by atula at cs.keele.ac.uk (Atula Herath):
>>Could somebody please explain me follwing para, from the K&R.
>>Page 197, para 7: [.....]
>>What does that mean ?

>.....
>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. Attempting to use the same
>member name in two places would therefore usually involve trying to assign two
>different type and offset values to that name (clearly not allowed)....


Another reason: K&R C (and ANSI[??] -- I don't know) allow[ed|s] a pointer
to be implicitly cast to a pointer-to-struct if the appropriate "->field"
was given.  Something like the following:

	typedef struct {int squiggle, wiggle;} worm_t;
	main()
	{	char	*p;
		...
		p->squiggle = 3;  /* implicit cast of "p" to "(worm_t *)p" */
		...
	}

I personally have never used this, as it seems left over from the days when
typedef's were discouraged (what some call "Classic C").  Now that type
definitions are usually fairly short, a cast is easy.  Naturally the most
important reason of all is that the "new" way makes the code more readable.
Lastly, K&R appendix A listed it obscurely, so I was never sure how long it
would last...

Anyway, that's probably another reason early fields in a struct had to have
the same type/offset and that all the field names in all the structs had to
be unique.

Marshall
--
	________________________________________________________________
	Marshall P. Cline	ARPA:	cline at sun.soe.clarkson.edu
	ECE Department		UseNet:	uunet!sun.soe.clarkson.edu!cline
	Clarkson University	BitNet:	BH0W at CLUTX
	Potsdam, NY  13676	AT&T:	315-268-6591



More information about the Comp.lang.c mailing list