I don't know how to reference what I am trying to do, but here is the explanation of what I desire to do.
In one module file "mod1.f" I have defined the following type
type md_typ1 real, pointer, contiguous :: x1(:) integer, pointer, contiguous :: i1(:) real, pointer, contiguous :: x11 ! no rank integer, pointer, contiguous :: i11 ! no rank end type
These pointer need to be an alias of a section of a buffer. Each of these pointers have an associated defined name as characters.
This allows me then to do the following in a different module file:
if( input_name == 'x1') typ1% x1 => buf(i1:i2)
I want it to be a bit more generic, where I in mod1 define a subroutine that returns one of the pointers in md_typ1, so we instead can do:
call GIVE_ME_POINTER( input_name, ptr_corresponding_to_input_name) ptr_corresponding_to_input_name => buf(i1:i2) ! assume here that input_name is x1 and hence the ptr_corresponding_to_input_name should have been associated to typ1% x1
I got two issues here:
1) Is it correct to assume (if everything done correctly) that `ptr_corresponding_to_input_name` is implicitly updating my `typ1% x1` - so EVEN THOUGH `ptr_corresponding_to_input_name` GOES OUT OF SCOPE, `typ1% x1` is still referencing to `buf(i1:i2)` ??
2) How do I make such a routine that can return the pointer generic. My attempts is the following
subroutine GIVE_ME_POINTER( name_str, ptr_out) character(len=*), intent(in) :: name_str class(*), intent(out) :: ptr_out select case(name_str) case('x1') ptr_out => typ1% x1 case('i1') ptr_out => typ1% i1 !etc etc end select end subroutine
Is that legal in Fortran , especially w.r.t the use of class(*)
Thanks in advance,