• Aucun résultat trouvé

Array Constants and Array Constructors

An array constant is a declared constant defined with an array constructor which specifies values for the components of an array type.

An array constructor consists of a previously defined array type identifier and a list of values in square brackets. Each component of the array type must receive a value which is assignment compatible with the component type.

Array Constant

array type identifier

constant

constant structured

constant

Within the square brackets, the reserved word OF indicates that a value occurs repeat-edly. For example, 3 OF 5 assigns the integer value 5 to three successive array compo-nents. The symbols (. and .) can replace the left and right square brackets, respectively.

An array constant must not contain files.

Array constructors are only legal in a CONST section of a declaration part. They cannot appear in other sections or in executable statements.

An array constant can be used to initialize a variable in the executable part of a block.

You can also access individuai components of an array constant in the body of a block, but not in the definition of other constants (see the subtopic, Array Selector).

Values for all elements of the structured type must be specified and must have a type identical to the type of the corresponding elements.

16 ARRAY

Example Code

In the last example, the type of the array component is char, yet both string literals and characters appear in the constructor. This is one case where a value (string literal) is assignment compatible with the component type (char). Alternatively, you could write

colors

=

color_array['RED','YELLOW','BLUE'];

for the last constant definition.

The name of the previously declared literal string constant can be specified within a

coiors

=

color_array color_string[red] ; cOlor_string [yellow] ; color_string[blue] ];

ARRAY 17

Array Selector

An array selector accesses a component of an array. The selector follows an array designator and consists of an ordinal expression in square brackets.

array type replace the left and right brackets, respectively. The component of a multiple-dimension array can be selected in different ways (see example).

For a string or PAC type, an array selector accesses a single component of a string

Conform ant

Arrays

The conformant array feature allows arrays of various sizes to be passed to a single formal parameter of a routine. It also provides a mechanism for determining at runtime the indices with which the actual parameter was declared.

Formal Parameter List

Formal Parameter Type

Index Type

formal parameter type

Conformant arrays are defined within the formal parameter list of a procedure or function. The diagrams show the extended syntax for a formal parameter list, and the syntax for the definition (or "schemas") of conform ant arrays.

ARRAY 19

Conformant arrays can be passed by value or by reference.

Conformant arrays can be packed or unpacked. A "schema" is a organizational de-scription of a conformant array parameter. Packed schemas are limited to one index.

Unpacked schemas can have any number of indices. In a schema with multiple indices, the final array definition can be either packed or unpacked.

Conformant arrays cannot be PAC types.

An abbreviated syntax is allowed for specifying multi-dimensional conformant arrays.

The schema:

ARRAY [Index_type1] OF ARRAY [Index_type2] OF

ARRAY [Index_type3] OF Type_IO can be written as:

ARRAY [Index_type1; Index_type2; . . . ; Index_type3] OF Type_IO

The bound identifiers (the low bound id and the high bound id in the index type specification) are used to determine the indices of the actual parameter passed to the formal conformant array. Their values are set when the routine is entered, and they remain constant throughout that activation of the routine.

Bound identifiers are special objects. They are not constants and are not variables; thus they cannot be used in CaNST or TYPE definitions, cannot be assigned to, and cannot be used in any other context in which a variable is expected (such a.~ an actual VAR parameter, FOR loop control variable, etc.).

Conformability

An actual array parameter must "conform" to the corresponding formal parameter. An array variable can be passed to a routine with a corresponding formal conformant array parameter if the array variable's type, AT "conforms with" the schema, S, of the formal parameter.

An informal way of describing conformability is to say that AT conforms with S if, for each dimension of AT (and S), the index types and component types of AT and S match.

20 ARRAY

For a more formal definition, let

For example, given the following types and conformant array schemas:

Type:

Note

Single-character literals are never compatible with conformant formal parameters.

Equivalence

Two conformant array schemas are "equivalent" if all of the following are true:

• The ordinal type identifier in each corresponding index type specification denotes the same type.

• Either:

• the type identifier of the two schemas denotes the same type, or

• the component conformant array schemas of both schemas are equivalent.

Congruence

An actual array parameter of an actual procedure or function parameter must be

"congruent" with the corresponding formal parameter. Two conformant array schemas are "congruent" if all of the following are true:

• The two schemas are both packed or unpacked.

• The two schemas are both by-value or by-reference schemas.

• The two schemas are equivalent.

Example

PROGRAM show_conform (output);

CONST

PROCEDURE initialize_matrix

writeln('Small Matrix: ');

initialize_matrix (small);

writeln;

writeln('Large Matrix: ');

initialize_matrix (large);

END.

Inside the procedure, L01, Hi1, L02 and Hi2 can be used anywhere a variable or constant can be used, except:

• In declaration statements. That is, you cannot declare another variable such as:

var reference to a procedure or function.

ARRAY 23

To send multiple conformant arrays to a procedure (or function; all these statements about conformant arrays can be applied to function parameters, too), you just separate them by semicolons in the usual way. Also, you can intermix conformant arrays passed by value and conformant arrays passed by reference.

If you pass a conformant array to a procedure, and, from that procedure, you wish to pass the array to another procedure, you must pass it (the second time) by reference.

24 ARRAY

Assignment

An assignment statement assigns a value to a variable or a function result. The assignment statement consists of a variable or function identifier, an optional selector~ a special symbol (:=), and an expression which computes a value.

variable identifier

function identifier

expreSSlcr

The receiving element can be of any type except file, or a structured type containing a file type component. An appropriate selector permits assignment to a component of a structured variable or structured function result.

The type of the expression must be assignment compatible with the type of the receiving element (see below).

Types must be identical except when an implicit conversion is done, or a run-time check is performed which verifies that the value of the expression is assignable to the variable.

Assignment 25

Example Code

FUNCTION show_assign: integer;

TYPE

FUNCTION show_structured: rec;

BEGIN

show_structured.f := 20;

show_structured := cr;

{subscripted array variable, }

{record variable, }

Documents relatifs