Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Records, Arrays and Pointers

Records and Variant Records in Pascal

Group related data with Pascal records: declare fields, copy and compare records, use with statements, arrays of records, packed and variant records, and advanced records with methods.

An array holds many values of the same type. A record groups values of different types that belong together: a person has a name (text), an age (number) and a flag (Boolean). Records are Pascal's original structured type, the counterpart of C's struct, and they are still the best way to bundle plain data without the weight of a class.

Pascal listings are read-only here. The C stand-ins at the end use struct, which models the same layout.

Declaring and using records

PASCAL
program Records;

type
  TPerson = record
    Name: string;
    Age: Integer;
    IsMember: Boolean;
  end;

var
  p: TPerson;

begin
  p.Name := 'Ada';
  p.Age := 36;
  p.IsMember := True;
  WriteLn(p.Name, ' is ', p.Age);
end.

Output:

TEXT
Ada is 36

A record type lists fields, each with a name and a type. You reach a field with the dot: p.Name. Several fields of the same type can share a line: X, Y: Double;. Naming the type with a T prefix is the common convention.

Assigning a record copies every field:

PASCAL
var
  a, b: TPerson;
begin
  a.Name := 'Ada';
  a.Age := 36;
  b := a;             { copies all fields }
  b.Name := 'Bob';    { a.Name is still 'Ada' }
end.

Records cannot be compared with = directly (there is no built-in equality); compare the fields yourself.

Records as parameters and results

Because a record can be large, pass it as const when a routine only reads it, or var when the routine changes it. Functions can return records:

PASCAL
function MakePoint(x, y: Double): TPoint;
begin
  Result.X := x;
  Result.Y := y;
end;

procedure Birthday(var p: TPerson);
begin
  Inc(p.Age);
end;

Record constants

You can initialize a record constant with named fields:

PASCAL
const
  DefaultUser: TPerson = (Name: 'Guest'; Age: 0; IsMember: False);

The with statement

with lets you omit the record name inside a block:

PASCAL
with p do
begin
  Name := 'Grace';
  Age := 85;
end;

It saves typing but hurts readability and can silently bind names to the wrong place (a local variable called Name will lose to the field, or the other way round, depending on nesting). Most style guides advise against it; use short variable names instead.

Nested records and arrays of records

Fields may be records or arrays themselves:

PASCAL
type
  TDate = record
    Year, Month, Day: Integer;
  end;

  TEmployee = record
    Name: string;
    Hired: TDate;
    Salary: Double;
  end;

var
  staff: array[1..3] of TEmployee;
  i: Integer;
  total: Double;

begin
  staff[1].Name := 'Ana';
  staff[1].Hired.Year := 2020;
  staff[1].Salary := 4200;
  { ... staff[2], staff[3] ... }
  total := 0;
  for i := 1 to 3 do
    total := total + staff[i].Salary;
end.

An array of records is effectively a small in-memory table, and the Files and Databases chapter later in the course shows how to store them on disk.

Memory layout and packed records

The compiler may insert padding between fields so each one sits at a convenient address. A record with a Byte followed by an Integer can take 8 bytes, not 5. Writing packed record removes the padding, which matters when you must match a file or network format exactly, but access can be slower:

PASCAL
type
  TLoose  = record A: Byte; B: Integer; end;           { usually 8 bytes }
  TPacked = packed record A: Byte; B: Integer; end;    { 5 bytes }

SizeOf(TLoose) tells you the real size on your compiler.

Variant records

A variant record has a fixed part and a variant part whose fields share the same memory, so only one alternative is meaningful at a time. A tag field records which one:

PASCAL
type
  TShapeKind = (skCircle, skRect);

  TShape = record
    Name: string;
    case Kind: TShapeKind of
      skCircle: (Radius: Double);
      skRect:   (Width, Height: Double);
  end;

var
  s: TShape;
begin
  s.Name := 'Plate';
  s.Kind := skCircle;
  s.Radius := 12.5;
end.

Radius and Width occupy the same bytes. The compiler does not stop you from reading Width while Kind says circle, so keep the tag consistent. Variant records are Pascal's version of C's tagged unions; they save memory and are used for things like parsing binary data.

flowchart LR
    A["TShape"] --> B["Name (always)"]
    A --> C["Kind (tag)"]
    A --> D{"shared bytes"}
    D --> E["Radius"]
    D --> F["Width + Height"]

Figure: the two variants overlap in memory.

Advanced records

Delphi (and Free Pascal with {$modeswitch advancedrecords}, set automatically in Delphi mode) lets a record own methods and operator overloads, so it can behave like a small value type:

PASCAL
type
  TPoint2D = record
    X, Y: Double;
    function Length: Double;
    class operator Add(const A, B: TPoint2D): TPoint2D;
  end;

function TPoint2D.Length: Double;
begin
  Result := Sqrt(X * X + Y * Y);
end;

class operator TPoint2D.Add(const A, B: TPoint2D): TPoint2D;
begin
  Result.X := A.X + B.X;
  Result.Y := A.Y + B.Y;
end;

With that, c := a + b; and a.Length work. Classes, covered in the Classes and Properties chapter, are for objects with identity and inheritance; records are for small values copied freely.

Stand-ins in C

C's struct is the same idea, and a union inside a tagged struct is exactly a variant record. These programs are C.

C C stand-in: struct, copy and padding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C C stand-in: a variant record as a tagged union

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you access the Age field of a record variable p?

    Pascal uses the dot for record fields.
  2. 2. What does b := a do when a and b are records of the same type?

    Record assignment copies all fields. Afterwards the two variables are independent.
  3. 3. What is special about the variant part of a record?

    Variant fields overlap in memory. A tag field normally tells you which alternative is valid.
  4. 4. What does packed record change?

    Packing removes alignment padding. It matches external layouts but may slow field access.
  5. 5. Why do many style guides avoid the with statement?

    Names inside with can silently bind to a field instead of a variable you intended, which hurts readability.

Summary

  • A record groups fields of different types; use p.Field; assignment copies the whole record.
  • Pass large records as const or var; records can be nested and put in arrays.
  • packed removes padding; variant records overlap alternative fields behind a tag.
  • Advanced records add methods and operators to value types.

Next: Pointers and Dynamic Memory