# Fortran Arrays Whole arrays, array elements and array sections

## Example

Consider the array declared as

``````real x(10)
``````

Then we have three aspects of interest:

1. The whole array `x`;
2. Array elements, like `x(1)`;
3. Array sections, like `x(2:6)`.

## Whole arrays

In most cases the whole array `x` refers to all of the elements of the array as a single entity. It may appear in executable statements such as `print *, SUM(x)`, `print *, SIZE(x)` or `x=1`.

A whole array may reference arrays which aren't explicitly shaped (such as `x` above):

``````function f(y)
real, intent(out) :: y(:)
real, allocatable :: z(:)

y = 1.         ! Intrinsic assignment for the whole array
z = [1., 2.,]  ! Intrinsic assignment for the whole array, invoking allocation
end function
``````

An assumed-size array may also appear as a whole array, but in limited circumstances only (to be documented elsewhere).

## Array elements

An array element is referred to be giving integer indexes, one for each rank of the array, denoting the location in the whole array:

``````real x(5,2)
x(1,1) = 0.2
x(2,4) = 0.3
``````

An array element is a scalar.

## Array sections

An array section is a reference to a number of elements (perhaps just one) of a whole array, using a syntax involving colons:

``````real x(5,2)
x(:,1) = 0.         ! Referring to x(1,1), x(2,1), x(3,1), x(4,1) and x(5,1)
x(2,:) = 0.         ! Referring to x(2,1), x(2,2)
x(2:4,1) = 0.       ! Referring to x(2,1), x(3,1) and x(4,1)
x(2:3,1:2) = 0.     ! Referring to x(2,1), x(3,1), x(2,2) and x(3,2)
x(1:1,1) = 0.       ! Referring to x(1,1)
x([1,3,5],2) = 0.   ! Referring to x(1,2), x(3,2) and x(5,2)
``````

The final form above uses a vector subscript. This is subject to a number of restrictions beyond other array sections.

Each array section is itself an array, even when just one element is referenced. That is `x(1:1,1)` is an array of rank 1 and `x(1:1,1:1)` is an array of rank 2.

Array sections do not in general have an attribute of the whole array. In particular, where

``````real, allocatable :: x(:)
x = [1,2,3]     ! x is allocated as part of the assignment
x = [1,2,3,4]   ! x is dealloacted then allocated to a new shape in the assignment
``````

the assignment

``````x(:) = [1,2,3,4,5]   ! This is bad when x isn't the same shape as the right-hand side
``````

is not allowed: `x(:)`, although an array section with all elements of `x`, is not an allocatable array.

``````x(:) = [5,6,7,8]
``````

is fine when `x` is of the shape of the right-hand side.

## Array components of arrays

``````type t
real y(5)
end type t

type(t) x(2)
``````

We may also refer to whole arrays, array elements and array sections in more complicated settings.

From the above, `x` is a whole array. We also have

``````x(1)%y        ! A whole array
x(1)%y(1)     ! An array element
x%y(1)        ! An array section
x(1)%y(:)     ! An array section
x([1,2]%y(1)  ! An array section
x(1)%y(1:1)   ! An array section
``````

In such cases we are not allowed to have more than one part of the reference consisting of an array of rank 1. The following, for example, are not allowed

``````x%y             ! Both the x and y parts are arrays
x(1:1)%y(1:1)   ! Recall that each part is still an array section
`````` PDF - Download Fortran for free