Help, page 197 K&R !!!

Roger Critchlow rec at elf115.uu.net
Thu Jul 6 20:51:01 AEST 1989


In article <236100020 at mirror>, pat at mirror.TMC.COM writes:
>>/* 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.
>
>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?  

It got weirder still.  So long as the structure member had a unique
offset, the member could be referenced from any pointer or from any
lvalue.  Thus you could do this:

  is_an_int_value(l) long l;
  {
    struct halves { int high, low; };
  
    return (l.high == -1 || l.high == 0);
  }

If the programmer knew what he was doing, it should be okay?  But how
do _you_ figure out whether the programmer knew what he was doing when
you find his code after he's gone?  If the programmer knows what he's
doing, then he'll write the necessary casts, and get the word order
right on the first or second try.  :-)

-- rec at elf115.uu.net --



More information about the Comp.lang.c mailing list