Malcolm Cohen wrote:
> Almost. You need to have an array on the receiving side as well, i.e.
> the dummy argument ought to be declared
> CHARACTER*(N) C(1)
For those that remember this discussion.
I wanted to encapsulate this kind of muddy conversion between an array of
characters and a string inside a procedure. The first version of it was:
SUBROUTINE MakeCharacterPointer (character_pointer, character_array,
length)
INTEGER (KIND=i_wp), INTENT (IN) :: length
CHARACTER (KIND=C_CHAR, LEN=length), POINTER :: character_pointer
CHARACTER (KIND=C_CHAR, LEN=length), DIMENSION (1), TARGET ::
character_array
character_pointer => character_array (1)
END SUBROUTINE MakeCharacterPointer
one can then pass a rank-1 array of characters to it and get a string
pointer in return. In my codes I need to pass this pointer as an actual
argument for a dummy that is a CHARACTER(LEN=*) string. However, it seems to
me the above code does not guarantee it will work under the standard--if one
tries to pass an array section, even if contiguous, as in
character_array=contigous_array[5:]
the compiler may make a copy (Interp 125 business). So how about the
following?
SUBROUTINE MakeCharacterPointer (character_pointer, character_array,
length)
INTEGER (KIND=i_wp), INTENT (IN) :: length
CHARACTER (KIND=C_CHAR, LEN=length), POINTER :: character_pointer
CHARACTER (KIND=C_CHAR), DIMENSION (:), TARGET :: character_array
CALL MakeCharacterPointer_ (character_pointer, character_array(1),
length)
CONTAINS
SUBROUTINE MakeCharacterPointer_ (character_pointer,
character_array, length)
INTEGER (KIND=i_wp), INTENT (IN) :: length
CHARACTER (KIND=C_CHAR, LEN=length), POINTER :: character_pointer
CHARACTER (KIND=C_CHAR, LEN=*), TARGET :: character_array
character_pointer => character_array (1:length)
END SUBROUTINE MakeCharacterPointer_
END SUBROUTINE MakeCharacterPointer
This odd piece of code seems to compile, but what the guarantees are for it I
am not sure. It is assumed that the user will only pass contiguous actuals
for character_array.
Any comments or ideas on how to do this best?
Thanks,
Aleksandar
|