Help, page 197 K&R !!!

Ross Ridge ross at contact.uucp
Sun Jul 9 13:10:05 AEST 1989


In article <236100020 at mirror> pat at mirror.TMC.COM writes:
>
>This is one of the C paragraph responses:
>
>>/* Written  3:25 am  Jul  2, 1989 by gwyn at smoke.UUCP in mirror:comp.lang.c */
>>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.
>>/* 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 at contact
[oo]                   !                                      ! ross at ziebmef
-()-                   ! A glass of root-beer has half the    ! rridge at watmath
 db                    ! alcohol as a loaf of bread.          ! ross at watcsc



More information about the Comp.lang.c mailing list