042. Zero-size array constructor#

topic: Arrays

[data_type ::] is a zero-size array of type data_type. See examples below.

zero-size.f90 | | 0 | Godbolt Compiler Explorer logo | Fortran logo#
program zero_size
   implicit none

   associate ( &
      r0 => [real ::], &
      d0 => [real(kind(1.0d0)) ::], &
      i0 => [integer ::], &
      c20 => [character(len=2) ::], &
      c50 => [character(len=5) ::])

   print *, size(r0), kind(r0)   ! 0 4
   print *, size(d0), kind(d0)   ! 0 8
   print *, size(i0), kind(i0)   ! 0 4
   print *, size(c20), len(c20)  ! 0 2
   print *, size(c50), len(c50)  ! 0 5

   end associate  ! Required
end program zero_size
Output1#
           0           4
           0           8
           0           4
           0           2
           0           5

From the Tweet comments, one potential use for this is that “if a procedure has a required argument that is an integer array, you can pass [integer ::] from the caller.”

Note

The associate construct, used here, “yields simple abbreviations for more complex statements. It can be an alias for expressions or variables.”2



1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags

2

https://fortranwiki.org/fortran/show/associate