Ada arrays
last modified November 23, 2026
An array is a collection of values of one type, reached by an index. Ada separates the array type from the objects of that type: the type fixes the index type and, optionally, the bounds, while an object may supply its own bounds when the type leaves them open. This article declares array types, builds their initial values with aggregates, passes unconstrained arrays to subprograms, takes slices, and works with two-dimensional arrays.
Array types and aggregates
An array type is written array (index_range) of element_type. The
index range 1 to 5 makes a constrained type: every object of
Score_Array has exactly five elements. The initial value is an
aggregate, written in Ada 2022 with square brackets. Older Ada versions used
parentheses, which GNAT still accepts but reports as obsolescent under
-gnatwa.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Scores is
type Score_Array is array (1 .. 5) of Integer;
Marks : constant Score_Array := [80, 90, 70, 60, 95];
Total : Integer := 0;
begin
for I in Marks'Range loop
Total := Total + Marks (I);
end loop;
Put_Line ("total =" & Integer'Image (Total));
Put_Line ("average =" & Integer'Image (Total / Marks'Length));
end Scores;
The aggregate supplies the five elements in index order, so
Marks (1) is 80 and Marks (5) is 95. The loop uses
Marks'Range, which is the range 1 to 5 taken from the object rather
than written again. The total is 395, and integer division gives an average of
79. An index outside the bounds raises Constraint_Error.
$ gnatmake -gnat2022 -gnatwa scores.adb $ ./scores total = 395 average = 79
Index types and attributes
The index need not be an integer range. Any discrete type works: an integer
type, a range of characters, or an enumeration type. Using an enumeration as the
index makes the array read like a table and prevents the wrong kind of index
from being used. The attributes 'Range, 'First,
'Last, and 'Length describe the object's index.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Bounds is
type Days is (Mon, Tue, Wed, Thu, Fri);
type Hours is array (Days) of Integer;
Work : constant Hours :=
[Mon => 8, Tue => 8, Wed => 7, Thu => 8, Fri => 6];
begin
for D in Work'Range loop
Put_Line (Days'Image (D) & ":" & Integer'Image (Work (D)));
end loop;
Put_Line ("days =" & Natural'Image (Work'Length));
Put_Line ("first =" & Days'Image (Work'First));
Put_Line ("last =" & Days'Image (Work'Last));
end Bounds;
The aggregate names every day, and the loop visits them in their declaration
order. Work'First is Mon and Work'Last is
Fri, while Work'Length is 5. If a later change added a
day to the enumeration, Hours would gain an element, and the
compiler would require the aggregate to cover it.
$ gnatmake -gnat2022 -gnatwa bounds.adb $ ./bounds MON: 8 TUE: 8 WED: 7 THU: 8 FRI: 6 days = 5 first =MON last =FRI
Unconstrained array types
Writing range <> in the index leaves the bounds open, and the
type is then called unconstrained. An object of such a type takes its bounds
from its initial value, and a subprogram parameter of that type accepts an
array of any length. Inside the subprogram, 'Range gives the
bounds of the argument that was actually passed.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Vector_Demo is
type Vector is array (Positive range <>) of Integer;
function Sum (V : Vector) return Integer is
Total : Integer := 0;
begin
for I in V'Range loop
Total := Total + V (I);
end loop;
return Total;
end Sum;
A : constant Vector := [1, 2, 3];
B : constant Vector (1 .. 5) := [10, 20, 30, 40, 50];
begin
Put_Line ("a length =" & Natural'Image (A'Length));
Put_Line ("a sum =" & Integer'Image (Sum (A)));
Put_Line ("b sum =" & Integer'Image (Sum (B)));
end Vector_Demo;
A gets the bounds 1 to 3 from its three-element aggregate, while
B states its bounds explicitly as 1 to 5. The same
Sum works for both because the parameter is unconstrained. The
index subtype Positive rules out zero and negative indices.
$ gnatmake -gnat2022 -gnatwa vector_demo.adb $ ./vector_demo a length = 3 a sum = 6 b sum = 150
Aggregates with choices and others
An aggregate may associate values with particular indices and let one choice,
others, cover every position not named. A named choice is written
index => value. The same form fills a whole array with one
repeated value. This is useful for lookup tables and for initialising a buffer
before only some positions are overwritten.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Aggregates is
type Row is array (1 .. 6) of Integer;
Sparse : constant Row := [1 => 10, 3 => 30, others => 0];
Full : constant Row := [others => 7];
begin
for I in Sparse'Range loop
Put_Line (Integer'Image (I) & ":" & Integer'Image (Sparse (I)));
end loop;
Put_Line ("full first =" & Integer'Image (Full (1)));
Put_Line ("full last =" & Integer'Image (Full (6)));
end Aggregates;
Sparse stores 10 at index 1 and 30 at index 3; the other four
positions come from others and hold 0. Full uses
others alone, so every one of its six elements is 7. An aggregate
must cover every index exactly once, either directly or through
others, and the compiler rejects an aggregate that leaves a
position unaccounted for.
$ gnatmake -gnat2022 -gnatwa aggregates.adb $ ./aggregates 1: 10 2: 0 3: 30 4: 0 5: 0 6: 0 full first = 7 full last = 7
Slices
A slice selects a contiguous part of an array and is written with a range in
parentheses, as in V (2 .. 4). The slice is an array value in its
own right: it may be passed to a subprogram, assigned to another array, or
assigned a matching aggregate. A slice keeps the bounds of the range it was
taken from, so a slice of positions 2 to 4 still starts at index 2.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Slices is
type Vector is array (Positive range <>) of Integer;
V : Vector := [10, 20, 30, 40, 50];
Part : constant Vector := V (2 .. 4);
Total : Integer := 0;
begin
for I in Part'Range loop
Total := Total + Part (I);
end loop;
Put_Line ("part sum =" & Integer'Image (Total));
Put_Line ("part first =" & Positive'Image (Part'First));
V (2 .. 4) := [0, 0, 0];
Total := 0;
for I in V'Range loop
Total := Total + V (I);
end loop;
Put_Line ("new sum =" & Integer'Image (Total));
end Slices;
Part contains 20, 30, and 40 and keeps the bounds 2 to 4, which is
why Part'First is 2 even though the original vector starts at 1.
Assigning [0, 0, 0] to V (2 .. 4) replaces those three
elements and leaves the rest untouched, so the total falls from 150 to 60. The
aggregate on the right takes the bounds of the slice, and a different number of
values would be rejected.
$ gnatmake -gnat2022 -gnatwa slices.adb $ ./slices part sum = 90 part first = 2 new sum = 60
Multidimensional arrays
An array type may have more than one index, written as several ranges separated
by commas. The object is then indexed with one value per dimension, and
'Range (1) and 'Range (2) give the ranges of the
first and second dimensions. An aggregate for a two-dimensional array is an
array of rows, each row written as its own aggregate.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Matrix_Demo is
type Matrix is array (1 .. 3, 1 .. 3) of Integer;
M : constant Matrix := [1 => [1, 2, 3],
2 => [4, 5, 6],
3 => [7, 8, 9]];
Total : Integer := 0;
Diagonal : Integer := 0;
begin
for Row in M'Range (1) loop
for Col in M'Range (2) loop
Total := Total + M (Row, Col);
end loop;
end loop;
for I in M'Range (1) loop
Diagonal := Diagonal + M (I, I);
end loop;
Put_Line ("total =" & Integer'Image (Total));
Put_Line ("diagonal =" & Integer'Image (Diagonal));
end Matrix_Demo;
The nested loops visit all nine positions and produce a total of 45. The second loop uses the same index for both dimensions, which walks the main diagonal: 1, 5, and 9, for a sum of 15. The outer aggregate names rows 1 to 3, and each inner aggregate supplies the three values of one row.
$ gnatmake -gnat2022 -gnatwa matrix_demo.adb $ ./matrix_demo total = 45 diagonal = 15
An applied example: counting digits
An array may be indexed by a character range. The next program counts how often
each digit occurs in a string by using an array whose index subtype is
Character range '0' .. '9'. This is a small lookup table: the
character itself is the index, so no conversion to an integer is needed.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Digit_Count is
Text : constant String := "314159265358979";
type Counts is array (Character range '0' .. '9') of Natural;
Freq : Counts := [others => 0];
begin
for C of Text loop
if C in '0' .. '9' then
Freq (C) := Freq (C) + 1;
end if;
end loop;
for D in Freq'Range loop
Put_Line (D & ":" & Natural'Image (Freq (D)));
end loop;
end Digit_Count;
Freq starts with ten zeroes. Each character of the string that lies
between '0' and '9' increments the element at that
character's position in the index range. The final loop walks
Freq'Range, from '0' to '9', and prints
the count of each digit. The text contains fifteen digits, and the printed
counts add up to the same number.
$ gnatmake -gnat2022 -gnatwa digit_count.adb $ ./digit_count 0: 0 1: 2 2: 1 3: 2 4: 1 5: 3 6: 1 7: 1 8: 1 9: 3
Next steps
The next article, records, groups values of different types under one name. A record complements an array: an array reaches many values of the same type by position, while a record reaches a few named components of possibly different types. The two combine naturally in arrays of records, which the records article also covers.
Source
In this article we have declared constrained and unconstrained array types,
built aggregates with positional values, named choices, and others,
used enumeration and character index types, passed arrays and slices to
subprograms, and summed a two-dimensional matrix and its diagonal. The
strings article shows the same indexing and slicing
on the predefined String type.
Author
List all Ada tutorials.