I have a character string I would like to initialize with intent(out) data from a subroutine call. I kind of looks like that:
character(*) :: path
call get_path(path)
The compiler tells me:
Error: Entity with assumed character length at (1) must be a dummy argument or a PARAMETER
The construct works just fine in a subroutine but fails in the main program. Is it possible to initialize the path variable without knowing its length?
EDIT: Stuff I already tried but failed.
character(99) :: path_temp
character(:), allocatable :: path
call get_path(path_temp)
allocate(path(len(trim(path_temp))))
Error: Shape specification for allocatable scalar at (1)
I don't get why the compiler thinks path is a scalar.
A function that returns a character with assumed length apparently is illegal.
character(*) function get_path ()
get_path = '/path/to/folder/'
end function get_path
Error: Character-valued module procedure 'get_path' at (1) must not be assumed length
What works but gives me a headache because I find it very bad style is to give path an insane length and trim it every time it's used. I think my compiler is having trouble with allocatable character strings because it isn't quite up to date (mpif90). Not sure if it supports them.
get_path()a function returning the path as acharacter(len=*), instead of trying to accept it as an argument.pathhave an expected maximum length? Depending on the situation, I'll temporarily store a string of unknown length into a long, fixed-length variable, then usetrim()andadjustl()to strip whitespace before storing it in a variable-lengthallocatablecharacter variable.pathis a scalar because it is. It's a scalar character of deferred length, not a character array. To allocate such a scalar with lengthx, say, doallocate (character(x) :: path), or just dopath=trim(path_temp)and have the deferred lengthpathallocated on intrinsic assignment.