Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Pascal Dictionary
  3. Array Basics

Array Basics

In Pascal, arrays are declared using the array keyword. The index range is specified explicitly at declaration time, so out-of-range access can be detected at compile time or at runtime (when {$R+} is enabled). Any type may be used for elements, including integers, reals, strings, and records. Multidimensional arrays are declared as array of array of ... or array[range1, range2] of type.

Syntax

{ -----------------------------------------------
  One-dimensional array declaration (var section)
  ----------------------------------------------- }

var
  arr_name : array[low..high] of element_type;

{ Example: integer array of 5 elements (index 1 to 5) }
power_levels : array[1..5] of Integer;

{ -----------------------------------------------
  Two-dimensional array declaration
  ----------------------------------------------- }

var
  matrix_name : array[row_low..row_high, col_low..col_high] of element_type;

{ Example: 3x2 string array }
roster : array[1..3, 1..2] of String;

{ -----------------------------------------------
  Index access (assignment and read)
  ----------------------------------------------- }

arr_name[index] := value;          { write }
variable := arr_name[index];       { read }

{ Two-dimensional }
matrix_name[row_index, col_index] := value;

{ -----------------------------------------------
  Defining an array type in the TYPE section
  ----------------------------------------------- }

type
  TNameArray = array[1..5] of String;

var
  names : TNameArray;

Syntax Reference

Syntax / KeywordDescription
array[low..high] of typeDeclares a static array with the index range low..high. Integer types, enumerated types, and subrange types can be used as index types.
array[low..high, low..high] of typeDeclares a two-dimensional (or higher) array. Additional dimensions are added with commas.
arr[i]Accesses the element at index i. Used for both reading and writing.
arr[i, j]Accesses the element at row i, column j of a two-dimensional array.
Low(arr)Returns the minimum index of the array. Commonly used in loops to avoid hardcoding the lower bound.
High(arr)Returns the maximum index of the array. Used as in for i := Low(arr) to High(arr) do.
Length(arr)Returns the number of elements (High - Low + 1).
{$R+} directiveEnables range checking. Accessing an index outside the declared range causes a runtime error. Enabling this during development is common practice.

Sample Code

db_array_basic.pas

A sample demonstrating one-dimensional and two-dimensional array declaration and index access using Dragon Ball characters.

{ db_array_basic.pas — demonstrates Pascal array basics }
{ Uses Dragon Ball characters }
{ Covers 1D and 2D array declaration and index access }
{
  Compile and run:
    fpc db_array_basic.pas && ./db_array_basic
}

{$R+}

program db_array_basic;

const
  MEMBER_COUNT = 5;

type
  TNameArray       = array[1..MEMBER_COUNT] of String;
  TPowerLevelArray = array[1..MEMBER_COUNT] of LongInt;
  TProfileMatrix   = array[1..MEMBER_COUNT, 1..2] of String;

var
  names        : TNameArray;
  power_levels : TPowerLevelArray;
  profiles     : TProfileMatrix;
  i            : Integer;

begin
  names[1] := 'Son Goku';
  names[2] := 'Vegeta';
  names[3] := 'Piccolo';
  names[4] := 'Son Gohan';
  names[5] := 'Trunks';

  power_levels[1] := 9000000;
  power_levels[2] := 8000000;
  power_levels[3] := 3500000;
  power_levels[4] := 7500000;
  power_levels[5] := 6000000;

  WriteLn('===== Z Fighters Power Levels =====');
  for i := Low(names) to High(names) do begin
    WriteLn('  [', i, '] ', names[i], ' : ', power_levels[i]);
  end;
  WriteLn('');

  WriteLn('--- Array Info ---');
  WriteLn('  Low(names)    : ', Low(names));
  WriteLn('  High(names)   : ', High(names));
  WriteLn('  Length(names) : ', Length(names));
  WriteLn('');

  profiles[1, 1] := 'Son Goku';   profiles[1, 2] := 'Earth (born Saiyan)';
  profiles[2, 1] := 'Vegeta';     profiles[2, 2] := 'Planet Vegeta';
  profiles[3, 1] := 'Piccolo';    profiles[3, 2] := 'Earth (Namekian)';
  profiles[4, 1] := 'Son Gohan';  profiles[4, 2] := 'Earth';
  profiles[5, 1] := 'Trunks';     profiles[5, 2] := 'Future Earth';

  WriteLn('===== Z Fighters Profiles =====');
  for i := 1 to MEMBER_COUNT do begin
    WriteLn('  Name: ', profiles[i, 1], '  /  Origin: ', profiles[i, 2]);
  end;
  WriteLn('');

  WriteLn('--- Direct Access ---');
  WriteLn('  Strongest : ', names[1], ' (power: ', power_levels[1], ')');
  WriteLn('  Youngest  : ', names[5], ' (power: ', power_levels[5], ')');
  WriteLn('=========================================');
end.
fpc db_array_basic.pas && ./db_array_basic
Free Pascal Compiler version ...
Linking ./db_array_basic
===== Z Fighters Power Levels =====
  [1] Son Goku : 9000000
  [2] Vegeta : 8000000
  [3] Piccolo : 3500000
  [4] Son Gohan : 7500000
  [5] Trunks : 6000000

--- Array Info ---
  Low(names)    : 1
  High(names)   : 5
  Length(names) : 5

===== Z Fighters Profiles =====
  Name: Son Goku  /  Origin: Earth (born Saiyan)
  Name: Vegeta  /  Origin: Planet Vegeta
  Name: Piccolo  /  Origin: Earth (Namekian)
  Name: Son Gohan  /  Origin: Earth
  Name: Trunks  /  Origin: Future Earth

--- Direct Access ---
  Strongest : Son Goku (power: 9000000)
  Youngest  : Trunks (power: 6000000)
=========================================
db_array_enum_index.pas

Using an enumerated type as the array index. This makes the meaning of each element explicit.

{$R+}

program db_array_enum_index;

type
  TCharacter = (gcGoku, gcVegeta, gcPiccolo, gcGohan, gcTrunks);

  TPowerArray = array[TCharacter] of LongInt;
  TNameArray  = array[TCharacter] of String;

var
  power_levels : TPowerArray;
  names        : TNameArray;
  c            : TCharacter;

begin
  names[gcGoku]    := 'Son Goku';
  names[gcVegeta]  := 'Vegeta';
  names[gcPiccolo] := 'Piccolo';
  names[gcGohan]   := 'Son Gohan';
  names[gcTrunks]  := 'Trunks';

  power_levels[gcGoku]    := 9000000;
  power_levels[gcVegeta]  := 8000000;
  power_levels[gcPiccolo] := 3500000;
  power_levels[gcGohan]   := 7500000;
  power_levels[gcTrunks]  := 6000000;

  WriteLn('===== Enum-Indexed Array =====');
  for c := gcGoku to gcTrunks do begin
    WriteLn('  ', names[c], ' : ', power_levels[c]);
  end;
  WriteLn('=====================================');
end.
fpc db_array_enum_index.pas && ./db_array_enum_index
Free Pascal Compiler version ...
Linking ./db_array_enum_index
===== Enum-Indexed Array =====
  Son Goku : 9000000
  Vegeta : 8000000
  Piccolo : 3500000
  Son Gohan : 7500000
  Trunks : 6000000
=====================================
db_array_reverse.pas

Iterating an array in reverse order using Low and High.

{$R+}

program db_array_reverse;

const
  MEMBER_COUNT = 5;

type
  TNameArray = array[1..MEMBER_COUNT] of String;

var
  names : TNameArray;
  i     : Integer;

begin
  names[1] := 'Son Goku';
  names[2] := 'Vegeta';
  names[3] := 'Piccolo';
  names[4] := 'Son Gohan';
  names[5] := 'Trunks';

  WriteLn('===== Reverse Loop (High to Low) =====');
  for i := High(names) downto Low(names) do begin
    WriteLn('  [', i, '] ', names[i]);
  end;
  WriteLn('=======================================');
end.
fpc db_array_reverse.pas && ./db_array_reverse
Free Pascal Compiler version ...
Linking ./db_array_reverse
===== Reverse Loop (High to Low) =====
  [5] Trunks
  [4] Son Gohan
  [3] Piccolo
  [2] Vegeta
  [1] Son Goku
=======================================

Common Mistakes

Out-of-range access when {$R+} is disabled

Range checking is disabled by default. Accessing an out-of-range index causes neither a compile error nor a runtime error — the behavior is undefined. Adding {$R+} at the top of the file during development causes out-of-range access to be reported as a runtime error.

Confusing 0-based and 1-based indexing (off-by-one)

Pascal allows any integer range for array indices. For array[0..4], valid indices are 0 to 4; for array[1..5], they are 1 to 5. Coming from C where arrays always start at 0, accessing arr[0] on an array[1..5] array will cause a runtime error when {$R+} is enabled.

Overview

Pascal arrays are static: the index range is fixed at declaration time. The index can start at any integer — 0, 1, or any other value — though starting at 1 is a widely used convention. Using Low(), High(), and Length() avoids hardcoding bounds, reducing the cost of changing the array size later. Enabling {$R+} during development catches undefined behavior from index overflow at runtime. When a dynamically resizable array is needed, Free Pascal provides dynamic arrays (array of type) combined with SetLength(). For general type declarations see TYPE declarations; for loop syntax see for loop.

If you find any errors or copyright issues, please .