I’ve been trying to determine how to identify arrays and strings in code. I thought that you had to use the keyword ‘dimension’ to declare an array. But, I found what I think to be a counter argument against this, see below and explain.
Basically, my question is. How do you determine when a variable is an array and when a variable is a string?
Program Test
character :: C (3) = ['A', 'B', 'C']
Write (*,*)C
C(1) = 'D'
Write (*,*)C
End Program Test
Thank you
I am not sure I understand your question. AFAIK arrays can me defined either with the dimension(...)
keyword or using the parenthesis after a variable name var(3, 4, size(a))
. That is true irrespective of the variable type i.e. integer, real, character, etc.
In the MWE you have provided C is array, you can specify the len
variable when declaring C
which it would determine its length e.g.
character(len=3) :: C = 'abc'
For determining the type of a variable programmatically, I am sure there is a way that I cannot recall of the top of my head, but if you are interested in just finding out the variable type while coding you can just use a tool like fortls
, which should give you information about symbols when hovering, or looking at the symbols table.
Full disclosure I am the author of fortls
from Page Redirection
“We can declare arrays of any type. There are two common notations for declaring array variables: using the dimension
attribute or by appending the array dimensions in parentheses to the variable name.”
The RANK
intrinsic in F2018 is perhaps a better choice - it returns zero for a scalar. But I am struggling to come up with a use case other than for a procedure with an assumed-rank argument.
1 Like