传递数组长度以实现功能 [英] Passing array length to function

查看:112
本文介绍了传递数组长度以实现功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个微不足道的问题,我很抱歉。我的fortran-fu很差。

fortran中有一种路径来存放数组长度吗?使用 common (我收集的内容相当于 global )也是一个选项。我想要的是在主程序中调用带数组的函数。例如(这是输入的,而不是从任何地方复制粘贴)

 程序编制
整数编号
双精度x(num),v
双精度测试

....
v = test(x,num)
....


函数test(x,num)
双精度测试
整数num
双精度x(num)
测试= 0.0d0
....
return
end

num 不是一个常数。重要的是要知道我传递的数组大小。



编辑:我使用f95编译器。



Edit2:

 程序编程
整数v
参数(v = 10)
双精度x v),test,k
k = test(x)
write(*,*)size(x)
stop
end

function test x)
双精度,维(:),intent(in):: x
双精度测试
写(*,*)大小(x)
测试= 0.0d0
return
end

输出应该是两行,其中10和10是书面。相反,我得到了这个:

  / scpc:niels:#$ f95 err.f 
/ scpc:niels:# $ ./a.out
0
10
/ scpc:niels:#$


<解决方案Fortran数组'知道'它们有多长,你不需要传递数组和它的长度作为不同的参数。 (除非您正在与旧的Fortran代码进行交互)。今天,您会写这样的内容:

  function test arr)
real,dimension(:),intent(in):: arr
...
integer :: arrsize
...
arrsize = size( arr)
...

如果您必须与旧代码进行接口,通过你可以打电话这样

 调用old_fortran_subr(array,size(array,1),other_arguments)

哦,在我写作的时候,与 common 在你从头开始编写的任何代码中,它是70年代以前的(正确)弃用的功能。请使用模块变量


I'm sorry if this is a trivial question. My fortran-fu is poor.

Is there a way in fortran to path the array length? Using common (which, from what I gather is equivalent to global) is an option as well. What I want is in the main program to call to a function with an array. For example (This is typed in, not copy paste from anywhere)

program prog
integer num
double precision x(num),v
double precision test

....
v=test(x,num)
....


function test(x,num)
double precision test
integer num
double precision x(num)
test=0.0d0
....
return
end

This won't compile since num is not a constant. The important thing is to know what it the array size I'm passing.

Edit: I'm using f95 compiler.

Edit2: I tried High Performance Mark's solution without luck:

  program prog
  integer v
  parameter (v=10)
  double precision x(v),test,k
  k=test(x)
  write (*,*) size(x)
  stop
  end

  function test(x)
  double precision, dimension(:),intent(in) :: x
  double precision test
  write (*,*) size(x)
  test = 0.0d0
  return
  end

The output should be two lines where 10 and 10 are written. Instead I got this:

/scpc:niels: #$ f95 err.f
/scpc:niels: #$ ./a.out 
           0
          10
/scpc:niels: #$ 

解决方案

Fortran arrays 'know' how long they are, you shouldn't need to pass the array and its length as different arguments. (Unless, that is, you are interfacing with old Fortran codes.) Today you'd write something like this

function test(arr)
    real, dimension(:), intent(in) :: arr
    ...
    integer :: arrsize
    ...
    arrsize = size(arr)
    ...

If you have to interface to old code in which array sizes are passed you can make calls like this

  call old_fortran_subr(array, size(array, 1), other_arguments)

Oh, and while I'm writing, have nothing to do with common in any code you write from scratch, it's a (rightly) deprecated feature from the 70's and earlier. Instead use module variables.

这篇关于传递数组长度以实现功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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