In type definitions:
record_type_identifier = record
field_identifier: type_definition
...
field_identifier: type_definition
end;
or, with a variant part,
record_type_identifier = record
field_identifier: type_definition
...
field_identifier: type_definition
case bar: variant_type of
selector: (field_declarations);
selector: (field_declarations);
...
end;
or, without a variant selector field,
record_type_identifier = record
field_identifier: type_definition
...
field_identifier: type_definition
case variant_type of
selector: (field_declarations);
selector: (field_declarations);
...
end;
The reserved word record starts the definition of a new record type.
Records can be packed to save memory usage at the expense of speed.
The variants of a variant record may – but are not required to – share one location in memory (inside the record).
Sometimes variant records are used to emulate type casting in ISO 7185 Pascal. This is in fact a violation of the standard and not portable. There is intentionally no possibility in ISO 7185 Pascal to emulate type casting.
The reserved word record and record types are defined in ISO 7185 Pascal.
According to ISO Pascal, the variant type must be an identifier. GNU Pascal, like UCSD and Borland Pascal, also allows a subrange here.
Subranges in the variant fields, e.g. case Integer of 2 .. 5,
are a GPC extension.
program RecordDemo;
type
FooPtr = ^Foo;
Foo = record
Bar: Integer;
NextFoo: FooPtr;
case Choice: 1 .. 3 of
1: (a: Integer); { These three choices may share }
2: (b: Real); { one location in memory. }
3: (c: Char;
d: Boolean);
end;
Int5 = Integer attribute (Size = 5);
SmallFoo = packed record
b: 0 .. 3;
a: Int5;
r: Boolean
end; { needs 1 byte }
var
f: Foo;
begin
f.b := 3.14;
WriteLn (f.a) { yields some strange number which is part of the }
{ internal representation of the real number `f.b'. }
end.