Previous Topic Next topic Print topic


Arrays of Structures

Structures, like other variables, can be declared as arrays and can contain arrays as members. For example:

DECLARE 1 S(5),
      2 A FIXED BINARY,
      2 B(4) FLOAT BINARY,
      2 T(3),
            3 P POINTER,
            3 Q(6) CHARACTER(10);

In this example, S is an array of 5 structures. Each structure contains a fixed- point member followed by an array of 4 floating-point values, followed by an array of 3 substructures. Each substructure contains a pointer variable followed by an array of 6 character-string variables. The entire structure contains 5 occurrences of A, 20 occurrences of B, 15 occurrences of T, 15 occurrences of P, and 90 occurrences of Q.

Each member of a structure array is itself an array because there exist as many instances of the member as there are elements in the structure array. If a member is an array in its own right because it has its own dimension information (as do B, T, and Q in the previous example), the array inherits the additional dimensions from its containing structures. For instance, in the previous example, Q is a three-dimensional array whose bounds are (5,3,6).

Any member of a dimensioned structure is an array and must be referenced using a subscript for each of its dimensions. The subscripts may be written anywhere within the structure-qualified reference, but it is preferable to write each subscript immediately following the name to which it applies. Using the previous example to illustrate this recommended programming practice:

S(K) .A is a reference to the Kth element of A
S(K) .B(J) is equivalent to B (K, J)
S(K) .T(J) .Q(I)      is equivalent to S.T.Q (K, J, I)

or S.T(K,J) .Q(I)

or S(K,J, I) .T.Q,

It is not possible to use an asterisk to reference an entire extent (cross section) of a structure array.

Members of dimensioned structures can be used only as arrays in stream I/O and as arguments to array parameters whose bounds have been specified as asterisks. They cannot be assigned, used in record I/O, used in ADDR or DEFINED, or passed to parameters with constant bounds.

Subscripted references to members of dimensioned structures can be used in any context that permits a variable reference.

Previous Topic Next topic Print topic