modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Type declarations

Array types

An array type is a sequence of components of some other type.  The components of an array are distinguished by an index, which also has a specific type.  You can define arrays of arrays to represent two or greater than two-dimensional structures.

To define arrays, use:

ARRAY indextype {,indextype} OF elementtype

Each indextype must be BOOLEAN, CHAR, an enumeration type, or a subrange type.  A component of the array is defined for each combination of index values.

The elementtype can be any type, including array types.  An array type with multiple indextypes is simply shorthand for an array type with an array type as the element type.

Examples:

ARRAY [1..10] OF REAL;
ARRAY INTEGER[1..10] OF REAL;
ARRAY CHAR OF BOOLEAN;
ARRAY [1..10], [1..10] OF REAL;

Subscripting

You refer to a component of an array by subscripting an array variable. Subscripting is specified as follows:

variable[expression{,expression}]

The expressions must be assignable to the index types with which the array variable was declared.  The values of the expressions must lie within the range specified by the index type.

If an index expression is outside the range of the index type, and the module was compiled with index checking turned on, the program generates a runtime error.


Source:

  • Wirth N: Programming in Modula-2, 3rd ed. Springer Verlag, Berlin, 1985.
  • Stony Brook Modula-2 documentation. Used with permission. Note: Stony-Brook M2 offers an extended syntax with features not described here. Stony Brook M2 users are encouraged to visit the Stony Brook website and to consult the Stony Brook help system.