[comp.lang.pascal] Type cast byte/word in a record

ZCCBJSB%EB0UB011.BITNET@cunyvm.cuny.edu (Josep Sau B.) (04/25/91)

Alan mead <amead@s.psych.uiuc.edu> asked about accessing
fields in a record as bytes or as words...

Well, the cleanest way would be using a variant record,
if know in advance that words will be always aligned
in odd or in even bytes, like:

 Ambiguous: RECORD
   CASE BOOLEAN OF
   FALSE :( ByteArr : ARRAY 1..Siz! OF BYTE);
   TRUE  :( WordArr : ARRAY 1..Siz DIV 2! OF WORD);
 END;

But if you need to access to words aligned to any byte, like
Mem and MemW allow, you can simply do a typecast everytime
you need to access (read, assign...) two bytes as a word.
Example:

 Word(Ambiguous.ByteArr1!)  accesses bytes 1 and 2 as a word
 Word(Ambiguous.ByteArr2!)  accesses bytes 2 and 3 as a word
 ...

You have to bother about the order of least and most signifiant
byte in a word, which is 80x8x implementation dependent.

--Josep Sau