2

Is there a way in Fortran to omit the size when declaring a named constant array? I just find it unnecessary to count array elements.

I have something like the following in mind

integer, parameter :: a(???) = [1, 2, 1, 2, 1, 4]

What about constant strings?

character(len=???), parameter :: a = "Hello world. This is a very long string."

The obvious solution I see is to just use a really large number here (e.g. 1024) and call trim on it whenever we want to access it. This just doesn't seem as nice as it could be...


Note: I think that using the preprocessor (defining preprocessor macro and calling len on it) is not an elegant way here.

francescalus
  • 30,576
  • 16
  • 61
  • 96
jack
  • 1,658
  • 1
  • 7
  • 18

1 Answers1

2

You can use an implied shape array for the first case, and similar syntax for the character case

ijb@ijb-Latitude-5410:~/work/stack$ cat parm.f90
Program parm

  Implicit None ( External, Type )

  Integer, Dimension( 1:* ), Parameter :: a = [1, 2, 1, 2, 1, 4]

  Character(len=* ), Parameter :: b = "Hello world. This is a very long string."

  Write( *, * ) a, b
  
End Program parm
ijb@ijb-Latitude-5410:~/work/stack$ gfortran --version
GNU Fortran (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ijb@ijb-Latitude-5410:~/work/stack$ gfortran -Wall -Wextra -fcheck=all -std=f2018 -g -O parm.f90 
ijb@ijb-Latitude-5410:~/work/stack$ ./a.out
           1           2           1           2           1           4 Hello world. This is a very long string.
Ian Bush
  • 6,996
  • 1
  • 21
  • 27