指针分配期间的 fortran 90 预期边界规范 [英] fortran 90 expected bounds specification during pointer assignment

查看:74
本文介绍了指针分配期间的 fortran 90 预期边界规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Fortran 新手.我正在 Fortran 90 中编写一个程序来获取数组的非零元素并使用指针函数将它们放入一个新数组中,如下所示:

I am new to Fortran. I am writing a program in Fortran 90 to get non-zero elements of an array and put them into a new array using pointer function as following:

program prog
    implicit none
    integer, target :: a(5)
    integer :: i
    integer, pointer :: nz(:)


    a(1) = 1
    a(2) = 0
    a(3) = 0
    a(4) = 2
    a(5) = 3

    nz => non_zeros(a)
    do i=1, size(nz)
       write(*,*) nz(i)
    end do  

contains
function non_zeros(a)
    integer, target :: a(:) 
    integer, pointer:: non_zeros(:)
    integer :: n, i, j

    n = count(a .ne. 0)
    allocate(non_zeros(n))

    j = 0
    do i=1, m
        if (a(i) .ne. 0) then
            j = j + 1
            non_zeros(j) => a(i)
        end if
    end do  
end function non_zeros

end program prog

在编译过程中出现错误:

during compiling I got the error:

 non_zeros(j) => a(i)
 1
 Error: Expected bounds specification for 'non_zeros' at (1)

你能告诉我我做错了什么吗?提前致谢!

Can you please tell me what did I do wrong? Thank you in advance!

更新我的问题:根据High Performance Mark的解释,我定义了一个派生类型:

Update of my question: According to the explanation of High Performance Mark, I defined a derived type:

program prog
    implicit none
    integer, target :: a(5)
    type dt
        integer, pointer :: x
    end type
    type(dt), allocatable :: nz(:)


    a(1) = 1
    a(2) = 0
    a(3) = 0
    a(4) = 2
    a(5) = 3

    nz = non_zeros(a)

    contains

    function non_zeros(a)
        integer, target :: a(:) 
        type(dt), allocatable :: non_zeros(:)
        integer :: n, i, j

        n = count(a .ne. 0)
        allocate(non_zeros(n))

        j = 0
        do i=1, m
            if (a(i) .ne. 0) then
                j = j + 1
                non_zeros(j)%x => a(i)
            end if
        end do  

    end function non_zeros  
end program prog

现在程序可以运行并提供所需的结果.但是,在这种情况下我没有使用指针函数,因为我的函数返回一个可分配的指针数组,而不是指向数组的指针.有没有办法在这里使用指针函数?谢谢

Now program works and gives the desired results. However, I did not use pointer function in this case, since my function returns an allocatable array of pointers, not pointer to an array. Is there any way to use pointer function here? Thank you

推荐答案

要将 a 的非零元素放入新数组中,您只需声明

To get the non-zero elements of a into a new array you could simply declare

integer, dimension(:), allocatable :: non_zeros

然后用语句填充它

non_zeros = pack(a,a/=0)

并避免完全摆弄指针.这依赖于 2003 标准中引入的一项功能,但市场上所有(我认为)当前的 Fortran 编译器都实现了它.

and avoid fiddling around with pointers entirely. This relies on a feature introduced in the 2003 standard, but it is implemented by all (I think) the current crop of Fortran compilers on the market.

您编写的代码在我看来好像您希望 nz 是一个指针数组,nz 中的每个元素都指向一个非零元素a.如果我是对的,您误解了诸如

The code that you have written looks to me as if you want nz to be an array of pointers, with each element in nz pointing to a non-zero element of a. If I'm right, you've misunderstood what a statement such as

integer, pointer :: nz(:)

声明.它不声明指向整数的指针数组,而是声明指向整数数组的指针.当你写

declares. It does not declare an array of pointers to integers, it declares a pointer to an array of integers. When you write

non_zeros(j) => a(i)

您犯了错误,试图将 non_zeros 的元素设置为指向 a 的元素.

you're making the mistake of trying to set an element of non_zeros to point to an element of a.

此处的错误消息具有误导性,因为编译器将 non_zeros(j) 解释为语法错误的 bounds-specbounds-remapping,但错误是语义的,编译器不理解你对Fortran的误解.

The error message is misleading here because the compiler interprets non_zeros(j) as a syntactically-incorrect bounds-spec or bounds-remapping, but the error is semantic, the compiler doesn't understand your misunderstanding of Fortran.

这篇关于指针分配期间的 fortran 90 预期边界规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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