Array declaration in FORTRAN for beginners

北战南征 提交于 2019-12-20 02:49:26

问题


This is a beginners question but I haven't found a comprehensive answer.

What are the differences (if any) of the following declarations?

CHARACTER(5) :: a
CHARACTER, DIMENSION (5) :: b
CHARACTER(LEN=5) :: c
CHARACTER :: d(5)
CHARACTER :: e*5

And: are all of these declarations possible with other types, e.g REAL?


回答1:


Regardless of the type, <type>,dimension(5) :: b and <type> :: b(5) are identical and denote an array of length 5. <type> can be e.g. character, integer, real, logical, etc.

character(5) is a short-hand of character(len=5) and declares a string of length 5. If the length is omitted, it is assumed to be on. character :: d(5) is an array of five length-1 strings.

character :: e*5 is an older variant to specify the string length.

len is intrinsic to strings (and makes no sence for e.g. floats). You can specify your own derived types to have an length len, though ("Parameterized derived types"). For integers and floats (and some others) you can specify the kind of the variable in a similar way.

For details consult the Fortran 2008 Standard, Ch. 4.4.3.2 "Character type specifier".




回答2:


Three of those declare a character variable that holds a string of 5 characters. Two of them declare an array of 5 characters, each capable of holding a single character.

The two that declare arrays of 5 elements will work the same way to declare an array of 5 reals. The three that declare characters of length 5 have no analog in other variable types. The specific syntax of a subset of those three will compile, however, but will be used to select different kinds of reals, rather than denoting a character length.



来源:https://stackoverflow.com/questions/28863369/array-declaration-in-fortran-for-beginners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!