Pascal arrays
last modified November 18, 2026 Free Pascal 3.2.2
An array is a fixed-size collection of elements of the same type. Pascal arrays
are declared with a range of indices, and the range does not have to start at
zero: array[1..10] of Integer is as natural as
array[0..9] of Integer. This article describes static arrays,
their bounds, array constants, multidimensional arrays, array parameters, and
the range checks that catch an index outside the declared range.
Declaring and filling an array
The declaration writes the index range in square brackets after the keyword
array, followed by of and the element type. The
elements are accessed with the same square brackets, and the index must be an
ordinal value inside the range. The next program fills five elements and adds
them up.
program static;
{$mode objfpc}{$H+}
var
scores: array[1..5] of Integer;
i, total: Integer;
begin
for i := 1 to 5 do
scores[i] := i * 10;
total := 0;
for i := Low(scores) to High(scores) do
begin
if i > Low(scores) then
Write(' ');
Write(scores[i]);
total := total + scores[i];
end;
WriteLn;
WriteLn('total = ', total);
end.
The first loop uses the literal range, and the second uses
Low and High, which return the bounds of the array.
The elements are 10, 20, 30, 40, and 50, so the total is 150. The array itself
is stored inline in the program's data segment; no heap allocation is
involved.
$ fpc -gl static.pas $ ./static 10 20 30 40 50 total = 150
Bounds, Low, High, and Length
The index range is part of the type. Two arrays with different ranges are
different types even when their element type is the same. Low
returns the lower bound, High the upper bound, and
Length the number of elements. For a static array,
Length is High - Low + 1, and SizeOf is
the number of elements multiplied by the size of one element.
program bounds;
{$mode objfpc}{$H+}
var
a: array[0..4] of Integer;
b: array[2..6] of Integer;
begin
WriteLn('a: Low = ', Low(a), ', High = ', High(a), ', Length = ', Length(a));
WriteLn('b: Low = ', Low(b), ', High = ', High(b), ', Length = ', Length(b));
WriteLn('SizeOf(a) = ', SizeOf(a), ' bytes');
end.
Both arrays hold five integers, but their indices run from 0 to 4 and from 2 to
6. Length is five in both cases, and SizeOf is
twenty bytes because an Integer is four bytes. A loop written with
Low and High works for either declaration without
change.
$ fpc -gl bounds.pas $ ./bounds a: Low = 0, High = 4, Length = 5 b: Low = 2, High = 6, Length = 5 SizeOf(a) = 20 bytes
Array constants
An array can be initialised with a parenthesised list of values in a
const section. The values must match the element type and their
number must match the range. The next program stores the days of the week and
prints them with their indices.
program constants;
{$mode objfpc}{$H+}
const
Days: array[1..7] of string = (
'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday');
var
i: Integer;
begin
for i := 1 to 7 do
WriteLn(i, ': ', Days[i]);
end.
The initialiser is written with one value per element and commas between them.
Pascal does not require a trailing comma after the last value. The same syntax
can initialise a variable with a typed constant, but an untyped
const is read-only and is the safer choice for a table that must
not change.
$ fpc -gl constants.pas $ ./constants 1: Monday 2: Tuesday 3: Wednesday 4: Thursday 5: Friday 6: Saturday 7: Sunday
Multidimensional arrays
A multidimensional array declares several index ranges separated by commas.
The element is addressed with the same number of indices, as in
m[i, j]. The first index selects the row and the second the column,
which matches the usual mathematical notation.
program matrix;
{$mode objfpc}{$H+}
var
m: array[1..2, 1..3] of Integer;
i, j: Integer;
begin
for i := 1 to 2 do
for j := 1 to 3 do
m[i, j] := i * 10 + j;
for i := 1 to 2 do
begin
for j := 1 to 3 do
Write(m[i, j]:4);
WriteLn;
end;
WriteLn('Length(m) = ', Length(m));
WriteLn('Length(m[1]) = ', Length(m[1]));
end.
The program fills a two-by-three matrix and prints it row by row. On a
multidimensional static array, Length(m) returns the size of the
first dimension and Length(m[1]) the size of one row. The memory is
still contiguous: the rows are stored one after another.
$ fpc -gl matrix.pas $ ./matrix 11 12 13 21 22 23 Length(m) = 2 Length(m[1]) = 3
Arrays as parameters
An array parameter can be declared with a fixed range, in which case the
argument must have exactly that type. A const parameter prevents
the procedure from changing the elements and lets the compiler pass the array
by reference without copying it. The next program defines a named array type
and sums its elements.
program param;
{$mode objfpc}{$H+}
type
TIntArray = array[1..5] of Integer;
function Sum(const a: TIntArray): Integer;
var
i: Integer;
begin
Result := 0;
for i := Low(a) to High(a) do
Result := Result + a[i];
end;
var
values: TIntArray;
i: Integer;
begin
for i := 1 to 5 do
values[i] := i * i;
WriteLn('sum = ', Sum(values));
end.
The type name is part of the interface of Sum: a caller cannot
pass an array[0..4] of Integer or an
array[1..6] of Integer, because those are different types. When
the size is not known in advance, use an open array parameter
array of Integer, which the
procedures article introduced.
$ fpc -gl param.pas $ ./param sum = 55
Range checking
An index outside the declared range is not rejected by default. The program
reads or writes memory beyond the array, which may corrupt other variables or
crash much later. The compiler option -Cr adds a check before every
indexed access, and the program then stops with runtime error 201 at the
offending line.
program outofbounds;
{$mode objfpc}{$H+}
var
a: array[1..3] of Integer;
i: Integer;
begin
for i := 1 to 3 do
a[i] := i;
i := 4;
WriteLn(a[i]);
end.
The array has three elements, and the last statement reads element 4. Compiled
without checks, the program prints whatever happens to follow the array in
memory. Compiled with -Cr, it stops before the read and reports
the line that contains the invalid index. During development, the check is
worth the small cost.
$ fpc -gl -Cr outofbounds.pas $ ./outofbounds Runtime error 201 at $0000000000401128 $0000000000401128 main, line 10 of outofbounds.pas $0000000000422F3C $ echo $? 201
Next steps
Static arrays have a size that is fixed at compile time. The dynamic arrays article shows arrays that can grow, shrink, and be returned from functions. The strings article treats a string as an indexed sequence of characters, and the records article shows how arrays and records are combined.
Source
In this article we have declared static arrays with ranges other than zero,
used Low, High, Length, and
SizeOf, initialised array constants, worked with two-dimensional
arrays, passed arrays to functions, and enabled range checking. The series
continues with Pascal dynamic arrays.
Author
List all Pascal tutorials.