6 Arrays


Most languages provide arrays of one sort of another. Those provided by Ada are most similar to Pascal's, with the inclusion of several very handy features.

Unconstrained arrays, dynamic arrays and array attributes are some of the extras offered.

Simple arrays

Generally when declaring an array, a type definition would be created first, and an array would be declared using this definition.

In general the declaration of an array has this form:

Some points to note:

* The index specification can be a type (e.g. pet).

* The index specification can be a range (e.g. 1..50).

Index values must be of a discrete type.

Anonymous arrays

Arrays can also be declared directly, without using a predefined type.

This is known as an anonymous array (as it has no explicit type) and is incompatable with other arrays - even those declared exactly the same. Also they cannot be used as parameters to subprograms. In general it is better to avoid them.

Accessing and setting arrays

To access an element of an array

It is instructive to note that accessing an Ada array is indistinguishable from calling a function in all respects.

To store a value into an array...

Array aggregates

The values of an array can all be assigned at once, using an aggregate. An aggregate must specify a value for every array element.

The values in the aggregate are assigned in order to the values in the array.

It is also possible to use a named version of the aggregate where the individual elements of the array are named.

It is illegal to combine both notations in the one aggregate.

Aggregates can also be used in declarations in exactly the same manner, using either notation.

A discrete range can also be included in the aggregate.

The others option is particuarly useful in setting all elements of an array to a given value. In these situations type qualification is often required.

Consider the following declarations:

The Ada language doesn't like this; if you mix the others option with either positional or named association then you have to qualify it with a type mark:

Constant arrays

Constant arrays can be defined. In this case all elements should be initialised through the use of an aggregate when declared.

Array attributes

Attributes associated with arrays are as follows.

If the array is multi dimensional then the index should be specified e.g. array_name'range(n) supplies the range for the nth index.

This facility is very useful for stepping through arrays

This guarantees that every element will be processed.

Unconstrained array types

Unconstrained array types allow us to declare array types that are identical in all respects to normal array types except one - we don't declare how long they are.

The declaration of an unconstrained array defines a class of arrays that have the same element type, the same index type and the same number of indices.

The <> represents a range that has to be specified when a variable of type string is declared (filling in the blank when declaring a variable).

The type string can be used to define a large class of character arrays, identical except in the number of elements in the array.

To create an actual array, we have to provide a index constraint for the type.

Here the index constraint is the range 1..20. The advantage of this is that all strings declared are of the same type, and can thus be used as parameter to subprograms. This extra level of abstraction allows for more generalised subprograms.

To process every element of an a variable that is derived from an unconstrained array type requires the use of array attributes such as a'range, a'first etc. as we cannot be sure what index values incoming arrays may have, such as below.

Unconstrained arrays are typically implemented with an object that stores the bounds, as well as a pointer to the actual array.

Standard array operations

There are several operations that can be applied to arrays as a whole and not just the individual components.


An entire array can be assigned the value of another array. Both arrays must be of the same type. If they are of the same unconstrained type then they both must have the same number of elements.

Test for equality, inequality

The tests for equality and inequality are available for (almost) every Ada type. Two arrays are equal if each element of the array is equal to the corresponding element of the other array.


Two arrays can be concatenated using the & operator.

Ordering operations

The operators <,<=,>,>= can all be applied to one dimensional arrays. They are of most benefit when comparing arrays of characters.

Dynamic arrays

The length of an array can be determined at run time, rather than being specified by the programmer when writing the program.

Note that this does not easily allow the user's input to decide the size of the array, and should not be thought of as a device for achieving this. The second example is much more typical of it's use.

to the index...