Allocatable character variables in Fortran

前端 未结 2 1316
一生所求
一生所求 2021-01-11 15:33

My code (stripped down to what I think is relevant for this question) is

PROGRAM test

IMPLICIT NONE

CHARACTER(len=37) input
CHARACTER(len=:), allocatable :         


        
相关标签:
2条回答
  • 2021-01-11 16:14

    This

    character (len=:), allocatable :: input_trim
    

    is certainly syntactically correct in Fortran 2003. You don't say what the error that gfortran raises is, so I can't comment on why it doesn't accept the line -- perhaps you have an old version of the compiler installed.

    With an up-to-date Fortran compiler (eg Intel Fortran v14.xxx) you don't need to allocate the character variable's size prior to assigning to it, you can simply write

    input_trim = trim(input)
    

    Note that

    read(*,*) input_trim
    

    won't work.

    0 讨论(0)
  • 2021-01-11 16:29

    With Absoft Fortran, this compiles (haven't run with it yet):

    character,dimension(:),allocatable::Line_for_IO !Metcalf,Reid,Cohen p. 107, w/mods
    if(allocated(Line_for_IO)) deallocate(Line_for_IO)
    allocate(Line_for_IO(7*n+40))
    
    0 讨论(0)
提交回复
热议问题