Array declarations and constructors
An array in Fortran is a value with a shape, not a pointer to the first element. The shape is part of the declaration, the whole value can be assigned and passed, and a constructor builds one from a list of values in one statement. This lesson declares arrays of several shapes, fills them with the three kinds of constructor, and asks for the numbers that describe a shape.
Declaring an array
The bounds go after the name, in parentheses: integer :: a(5) is five elements, a(1) to a(5). A named constant is the usual way to say the length, because the same name then sizes the loops and the arrays: integer, parameter :: n = 5 and integer :: b(n).
An array may also be declared with an initial value, which is a constructor or a scalar that is broadcast to every element. The declaration is the specification part of the unit, so the shape is fixed before the first statement runs.
The array constructor
Square brackets with a comma separated list build an array value: [10, 20, 30, 40, 50]. The list must be of one type, and the compiler converts each element to the type of the array it is assigned to. Before Fortran 2003 the form was (/ ... /), which still compiles and still appears in old code; the brackets are the modern spelling of the same thing.
The file arrays.f90:
program arrays
implicit none
integer, parameter :: n = 5
integer :: a(5), b(n), squares(n)
real :: c(3) = [1.5, 2.5, 3.5]
integer :: i
a = [10, 20, 30, 40, 50]
b = [(i * i, i = 1, n)]
squares = [(i ** 2, i = 1, n)]
print *, "a :", a
print *, "b :", b
print *, "squares:", squares
print *, "c :", c
print *, "size(a):", size(a)
end program arrays
gfortran -Wall -Wextra -std=f2018 arrays.f90 -o arrays
./arrays
a : 10 20 30 40 50
b : 1 4 9 16 25
squares: 1 4 9 16 25
c : 1.50000000 2.50000000 3.50000000
size(a): 5
The declaration of c shows the fourth way to fill an array: an initial value in the declaration. It is applied once, before the program starts, and it is the reason c needs no assignment in the body.
The implied DO
A constructor may hold an implied DO: an expression and a loop control in parentheses, repeated for every value of the control variable. [(i * i, i = 1, n)] builds the squares of one to five, which is the shortest way to fill an array from its index.
The loop control inside an implied DO is a counted loop and takes the same three numbers as a do: start, end and stride. The control variable is an ordinary integer of the program and keeps its value after the constructor, so a program that relies on it should not; declare a separate counter for anything that matters.
Types and character arrays
The type of a constructor follows its elements, so a list of literals without a suffix is a default integer or real regardless of the array it is assigned to. When the type matters, the typed form states it once: [integer(int64) :: 1, 2, 3].
The file kinds_arr.f90:
program kinds_arr
use iso_fortran_env, only: int64
implicit none
integer(int64) :: big(3)
character(len=4) :: names(3)
integer :: zeros(4)
big = [1_int64, 2_int64, 3_int64]
names = ["anna", "bob ", "carl"]
zeros = 0
print *, "big :", big
print *, "names :", names
print *, "zeros :", zeros
print *, "len :", len(names)
end program kinds_arr
gfortran -Wall -Wextra -std=f2018 kinds_arr.f90 -o kinds_arr
./kinds_arr
big : 1 2 3
names :annabob carl
zeros : 0 0 0 0
len : 4
Two lines need a warning. zeros = 0 is a scalar assigned to an array, which stores the value in every element; that is broadcasting and it works for any type. And the character array prints as one run of characters, because each element is padded to its declared length and the list-directed print adds nothing between them: anna, bob and carl give annabob carl. An array of strings needs a loop or a format to be readable, and the lesson on formatted output shows the format.
SIZE and SHAPE
- size(a) is the number of elements, and size(a, dim) the length along one dimension of a rank-2 array.
- shape(a) is an array of the lengths in each dimension, which for a rank-1 array is one number.
- lbound(a) and ubound(a) are the first and last index, which are 1 and size(a) unless the declaration said otherwise.
- rank(a) is the number of dimensions, which is what the next lesson uses to tell a vector from a matrix.
A loop over an array should count from 1 to size(a) or from lbound to ubound, and not to a literal that was correct when the array was smaller. The compiler cannot check a literal against a shape it does not know, and the run either reads garbage or stops on the bounds check.
Exercises
- Declare an array of ten integers and fill it with the implied DO [(2 * i, i = 1, 10)]. What is the last element?
- Change character(len=4) to len=6 and rebuild. What does the names line look like now?
- Print shape(a) and rank(a) for the first program.
- Build a real constructor from an integer list without a suffix and assign it to a real array. Does it build, and what does the array hold?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where is the shape of an explicit-shape array fixed?
Answer
In the declaration, before the first statement runs
The bounds are part of the type, so the shape is known at compilation.
-
What does
[10, 20, 30]build?Answer
An array of three integers
The square brackets are the array constructor and the result is an array value.
-
What does
[(i * i, i = 1, 4)]contain?Answer
1, 4, 9 and 16
The implied DO repeats the expression for every value of i.
-
What does
zeros = 0do for an array?Answer
It stores zero in every element
A scalar on the right is broadcast to the whole array.
-
What does this program print?
program p implicit none integer :: a(3) integer :: i a = [(2 * i, i = 1, 3)] print *, sum(a) end program pAnswer
12
The array holds 2, 4 and 6, and their sum is 12.
-
Why does a character array print as one run of characters?
Answer
Each element is padded to its length and the print adds nothing between them
List-directed output separates numeric items but concatenates character ones.
-
Which intrinsic gives the number of elements?
Answer
size
size counts elements; len gives the length of a character value.
-
What is
lboundofinteger :: a(5)?Answer
1
The default lower bound is one, and the next lesson shows how to change it.
-
What is the old spelling of the array constructor?
Answer
(/ ... /)
The slant-bracket form predates the square brackets and still compiles.
-
Which type does
[1, 2, 3]have before it is assigned?Answer
The default integer
The constructor is built from its literals, and the assignment may convert.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev