Interesting... could this be encapsulated, maybe? If you created a "class",
you could do this inside the module, as part of the constructor for the
string class (I think). I'm not that up on the nuances of using allocatable
arrays for returning values, but here goes:
module MyStrModule
interface assignment(=)
module procedure AssignChar2Str
end interface
contains
subroutine AssignChar2Str(s_out,s_in)
character(len=*),intent(in) :: s_in
character*1,allocatable,intent(in out) :: s_out
integer :: n
n = len(s_in)
allocate(s_out(n))
call internal_new(s_out,n)
! s_out is now an array as desired...
end subroutine AssignChar2Str
subroutine internal_new(str,n)
integer,intent(in) :: n
character*(n),intent(in out) :: str
! Do nothing, I guess?
end subroutine internal_new
end module MyStrModule
Usage:
program test
use MyStrModule
character*1,allocatable :: c(:)
c = "Hello World"
end program test
Would this work?
Alvaro Fernandez
|It's an ugly situation. Even the solution that I think probably
|best is an ugly one. I just think it is less ugly than the
|alternatives. You can take advantage of the storage association
|inherent in some forms of argument passing. This allows you
|to play equivalence-like games to interpret the same chunk of memory
|with different attributes (namely the arrayness and character length).
|YOU can't directly use equivalence on dynamically allocated things,
|but you can fake it with argument association. Do something like
|
| character*1, allocatable, c(:)
| ....allocate c to size n.
| call sub(c, n)
| ...
| subroutine sub(c,n)
| character*(n) :: c
| ... here you can work with c as a scalar string.
|
|Don't use assumed-length in the dummy. I don't think that works.
|Use explicit length as I've shown.
|
|--
|Richard Maine | Good judgment comes from experience;
[log in to unmask] | experience comes from bad judgment.
| | -- Mark Twain
|
|