Pascal - Packed Arrays
- Details
- Category: Chapter 3
- Published: Sunday, 14 April 2013 16:27
- Written by Sternas Stefanos
- Hits: 17257
These arrays are bit-packed, i.e., each character or truth values are stored in consecutive bytes instead of using one storage unit, usually a word (4 bytes or more).
Normally characters and Boolean values are stored in such a way that each character or truth value uses one storage unit like a word. This is called unpacked mode of data storage. Storage is fully utilized if characters are stored in consecutive bytes. This is called packed mode of data storage. Pascal allows the array data to be stored in packed mode.
1. Declaring Packed Arrays
Packed arrays are declared using the keywords packed array instead of array. For example:
type pArray: packed array[index-type1, index-type2,...] of element-type; var a: pArray;
The following example declares and uses a two dimensional packed array:
program packedarray; var a: packed array [0..3,0..3] of integer; i, j : integer; begin for i:=0 to 3 do for j:=0 to 3 do a[i,j]:= i * j; for i:=0 to 3 do begin for j:=0 to 3 do write(a[i,j]:2,' '); writeln; end; end.
When the above code is compiled and executed, it produces following result:
0 0 0 0 0 1 2 3 0 2 4 6 1 3 6 9