Path: utzoo!utgpu!jarvis.csri.toronto.edu!dptcdc!berner!contact!ross From: ross@contact.uucp (Ross Ridge) Newsgroups: comp.lang.c Subject: Re: Help, page 197 K&R !!! Message-ID: <1989Jul6.225001.14007@contact.uucp> Date: 9 Jul 89 03:10:05 GMT References: <646@kl-cs.UUCP> <236100020@mirror> Reply-To: ross@contact.uucp (Ross Ridge) Organization: Contact User Supported BBS. Toronto, Ontario. Lines: 50 In article <236100020@mirror> pat@mirror.TMC.COM writes: > >This is one of the C paragraph responses: > >>/* Written 3:25 am Jul 2, 1989 by gwyn@smoke.UUCP in mirror:comp.lang.c */ >>In article cline@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. >>/* End of text from mirror:comp.lang.c */ Well check out section 14.1 of K&R. It says that "any lvalue is allowed before . [dot operator]", and "Also, the experession before a -> is required only to be a pointer or an integer." Of course it also says, "Such constructs are non-portable." >I agree that this is weard and it is bad programming practice, but is >it possible that if the programmer knew what he was doing should it be >okay for him to do it? Well for one thing C compiliers that will accept this kind of nonsense aren't around much any more, and since the new ANSI standard certainly isn't going to allow it either, I can't see many programmers being able to use this "feature". But arguably any programmer that would use this practice probably doesn't know what he's doing anyways. Of course you can still do all the above with today's compilers (ANSI or otherwise) you just have to use casts. (it's stil just as implimentation dependent as before however) char *p; ((struct vic *) 0xd000)->bkg = ((struct colours *) p)->black; To do something like "int i; i.bkg = 2;" you probably have to do something like "((struct vic *) &i)->bkg = 2". The C language comes with no saftey belts, and that's one of it's great features, but I feel the secret to good C programming is to avoid the temptation to take too much advantage of it's flexibility. Ross Ridge -- l/ Hi! I'm a freem! ! Ross Ridge -- The Dreaded C64 User ! ross@contact [oo] ! ! ross@ziebmef -()- ! A glass of root-beer has half the ! rridge@watmath db ! alcohol as a loaf of bread. ! ross@watcsc