在 Fortran 中访问返回数组的元素 [英] Access elements of returned array in Fortran

查看:29
本文介绍了在 Fortran 中访问返回数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问从函数返回的数组元素?例如,shape() 返回一个整数数组.如何将该数组的元素与整数进行比较?以下内容无法编译:

How does one access an element of an array that is returned from a function? For example, shape() returns an array of integers. How does one compare an element of that array to an integer? The following does not compile:

integer :: a
integer, dimension(5) :: b

a = 5
if (a .eq. shape(b)) then
    print *, 'equal'
end if

错误是:

if (a .eq. shape(c)) then
    1
Error: IF clause at (1) requires a scalar LOGICAL expression

我知道这是因为 shape(c) 返回一个数组.然而,访问数组的元素似乎不可能像这样:shape(c)(1)

I understand that this is because shape(c) returns an array. However, accessing an element of the array does not appear to be possible like so: shape(c)(1)

现在如果我添加这两行:

Now if I add these two lines:

integer, dimension(1) :: c
c = shape(b)

...并将 if 子句更改为:

...and change the if clause to this:

if (a .eq. c(1)) then

...然后它起作用了.但是我真的必须声明一个额外的数组变量来保存 shape() 的返回值,还是有其他方法可以做到这一点?

... then it works. But do I really have to declare an extra array variable to hold the return value of shape(), or is there some other way to do it?

推荐答案

进一步处理 SHAPE 和逻辑表达式等的答案,对您的问题如何访问从一个函数?"是

Further to the answers that deal with SHAPE and logical expressions etc, the general answer to your question "How does one access an element of an array that is returned from a function?" is

  • 将具有函数引用的表达式分配给数组变量,然后索引该数组变量.

  • you assign the expression that has the function reference to an array variable, and then index that array variable.

您使用具有函数引用的表达式作为过程的实际参数,该过程采用虚拟数组参数,并为您进行索引.

you use the expression that has the function reference as an actual argument to a procedure that takes a dummy array argument, and does the indexing for you.

因此,您最后一个问题的一般答案是但我真的必须声明一个额外的数组变量来保存 shape() 的返回值,还是有其他方法可以做到这一点?"是是的,您确实需要声明另一个数组变量",因此不,没有其他方法".

Consequently, the general answer to your last questions "But do I really have to declare an extra array variable to hold the return value of shape(), or is there some other way to do it?" is "Yes, you do need to declare another array variable" and hence "No, there is no other way".

(请注意,合理的优化编译器一旦获得数组函数的结果,将避免对任何额外的内存操作/分配等的需要,这实际上只是一个语法问题.)

(Note that reasonable optimising compilers will avoid the need for any additional memory operations/allocations etc once they have the result of the array function, it's really just a syntax issue.)

语言设计的这一特定方面的基本原理有时归因于需要避免字符类型的数组函数结果的语法歧义和混淆(它们可能被索引和/或子字符串 - 你怎么知道是什么?故意的?).其他人认为这样做只是为了惹恼 C 程序员.

The rationale for this particular aspect of language design is sometimes ascribed to a need to avoid syntax ambiguity and confusion for array function results that are of character type (they could potentially be indexed and/or substringed - how do you tell what was intended?). Others think it was done this way just to annoy C programmers.

这篇关于在 Fortran 中访问返回数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆