Path: utzoo!attcan!uunet!image.soe.clarkson.edu!news From: cline@sun.soe.clarkson.edu (Marshall Cline) Newsgroups: comp.lang.c Subject: Re: Help, page 197 K&R !!! Message-ID: Date: 1 Jul 89 14:20:36 GMT References: <646@kl-cs.UUCP> <648@kl-cs.UUCP> Sender: news@sun.soe.clarkson.edu Reply-To: cline@sun.soe.clarkson.edu (Marshall Cline) Organization: Clarkson University, Postdam NY Lines: 47 In-reply-to: pc@cs.keele.ac.uk's message of 30 Jun 89 13:46:06 GMT In article <648@kl-cs.UUCP> pc@cs.keele.ac.uk (Phil Cornes) writes: >From article <646@kl-cs.UUCP>, by atula@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@sun.soe.clarkson.edu ECE Department UseNet: uunet!sun.soe.clarkson.edu!cline Clarkson University BitNet: BH0W@CLUTX Potsdam, NY 13676 AT&T: 315-268-6591