Access elements of returned array in Fortran -
how 1 access element of array returned function? example, shape()
returns array of integers. how 1 compare element of array integer? following not compile:
integer :: integer, dimension(5) :: b = 5 if (a .eq. shape(b)) print *, 'equal' end if
the error is:
if (a .eq. shape(c)) 1 error: if clause @ (1) requires scalar logical expression
i understand because shape(c)
returns array. however, accessing element of array not appear possible so: shape(c)(1)
now if add these 2 lines:
integer, dimension(1) :: c c = shape(b)
...and change if
clause this:
if (a .eq. c(1))
... works. have declare array variable hold return value of shape()
, or there other way it?
further answers deal shape , logical expressions etc, general answer question "how 1 access element of array returned function?" is
you assign expression has function reference array variable, , index array variable.
you use expression has function reference actual argument procedure takes dummy array argument, , indexing you.
consequently, general answer last questions "but have declare array variable hold return value of shape(), or there other way it?" "yes, need declare array variable" , hence "no, there no other way".
(note reasonable optimising compilers avoid need additional memory operations/allocations etc once have result of array function, it's syntax issue.)
the rationale particular aspect of language design ascribed need avoid syntax ambiguity , confusion array function results of character type (they potentially indexed and/or substringed - how tell intended?). others think done way annoy c programmers.
Comments
Post a Comment