What is the correct way to initialize a parameter array in fortran?
The square bracket form was added to the language in Fortran 2003. If you are writing to Fortran 90 (as per the tag on the question) then the square bracket form is a syntax error (square brackets are not in the Fortran 90 character set).
Beyond language standard it a question of personal preference and style.
mgilson
I used to be a fortran and sometimes C programmer, but these days I write mostly python and javascript. I am interested in computational physics and like to write code. I also used to be an avid gnuplot user and maybe someday I will be again... I am a currently a software engineer at Argo AI working to make the world's cars drive themselves. ~Matt
Updated on June 05, 2022Comments
-
mgilson almost 2 years
This works just fine:
program main integer,parameter,dimension(3) :: x = [1,2,3] print*,x end program main
As does this:
program main integer,parameter,dimension(3) :: x = (/1,2,3/) print*,x end program main
Is there a reason to think that one form should be preferred over the other (e.g. backward compatibility)?
-
mgilson about 11 yearsDo you know if there was any reason discussed as to why the language needed a second way to declare an array literal?
-
IanH about 11 yearsNot specifically. I suspect there was no pressing need, just a want, given the single character is more readable and their use is somewhat of a convention across languages.
-
mgilson about 11 yearsThat's kind of how I feel about it -- They looked at the language and said "Oops, we goofed up on this one the first time around ... Might as well make a second way to do it ...". sigh However, since I'm trying to target f90-f95, I suppose I'll stick with the ugly version. Thanks. +1 and checkmark to you.
-
Bálint Aradi about 11 yearsProbably they wanted to avoid the occurance of too many subsequent parantheses:
call somearrayfunc((/ 1, 2, 3 /))
versuscall somearrayfunc([ 1, 2, 3])
. For me at least, the 2nd form is more readable. -
max about 11 yearsI believe this has been introduced with Coarrays in mind.