type punning

Doug Gwyn (VLD/VMB) gwyn at BRL-VLD.ARPA
Sat Feb 9 00:35:50 AEST 1985


Sigh.  One can usually get away with punning different (struct XXX *)s
because in a given implementation struct pointers probably all have a
uniform representation.  On the other hand, head->id[3] where the member
id[] has only been declared of length 3 is illegal BECAUSE such usage
may not work in general (for adjacent char arrays it usually does, but
there is no guarantee of this in the language specification).  If you
happen to KNOW that you have e.g. a VAX, then of course you can cheat
on the rules and get away with it.  However, the same code may break
miserably if run on some other machine (say, an S-1).  To say that a
MACHINE is non-portable just because its architecture is not that of a
VAX strikes me as pretty silly.  Lots of us have a variety of machines
to cope with, and these VAX-specific tricks just get in the way.

By the way, there are other ways to code linked lists while following
the language rules.  The example you gave is woefully short on error
detection if a coding error is made and the wrong sort of structure
is linked into a list.



More information about the Comp.lang.c mailing list