"Dick Hendrickson" <
dick.hen...@att.net> wrote in message
news:a99qmg...@mid.individual.net...
> One thing you can do to view character variables as either arrays or
> strings is to pass them on to subroutines or functions. It's OK to pass a
> character array to a procedure and declare it as a scalar character
> variable of whatever length is appropriate.
This is close to the truth, but not quite in my (and gfortran's) reading
of the standard. N1830.pdf, section
12.5.2.4:
"If a scalar dummy argument is default character or of type character
with the C character kind, the length len of the dummy argument shall
be less than or equal to the length of the actual argument. The dummy
argument becomes associated with the leftmost len characters of the
actual argument. If an array dummy argument is default character or of
type character with the C character kind and is not assumed shape, it
becomes associated with the leftmost characters of the actual argument
element sequence (12.5.2.11)."
So to associate an array of size 10 and len 1 with a scalar dummy of
len 10 you would have to make an intermediate call to an array dummy
of size 1 and len 10 and then pass the first element of this array
dummy as an actual argument so that the second procedure now can be
a scalar of the desired length. Of course, it may be that it is
sufficient to work with an array of size 1 and len 10.
> Ditto, scalars can be turned
> into arrays across a call. (And arrays can be reshaped, etc.)
Now, this is OK.
> The only restriction is the obvious one that you can't make the variable
> bigger on the called side. It's a little bit of a pain in the tush if
> you're doing lots of manipulations in one expression. But if you need to
> do a bunch of stuff in scalar string mode and then a bunch of stuff in
> array mode, it's not so bad.
An example with pointers:
C:\gfortran\clf\long_string>type string3.f90
module sideways
implicit none
private
public a2s, s2a
contains
function a2s(A,N)
integer N
character(N), target :: A(1)
character(N), pointer :: a2s
a2s => A(1)
end function a2s
function s2a(S,N)
integer N
character, target :: S(N)
character, pointer :: s2a(:)
s2a => S
end function s2a
end module sideways
program string3
use sideways
implicit none
character(:), pointer :: S1
character, allocatable, target :: A1(:)
character(:), allocatable, target :: S2
character, pointer :: A2(:)
integer i
A1 = transfer('1234567890',A1)
S1 => a2s(A1,size(A1))
write(*,'(2a)') 'S1 = ', S1
do i = 1, len(S1)
write(*,'(2(a,i0),2a)') 'S1(',i,':',i,') = ', S1(i:i)
end do
S2 = '1234567890'
A2 => s2a(S2,len(S2))
write(*,'(11a)') 'A2 = ', A2
do i = 1, size(A2)
write(*,'(a,i0,2a)') 'A2(',i,') = ', A2(i)
end do
end program string3
C:\gfortran\clf\long_string>gfortran string3.f90 -std=f2008 -ostring3
C:\gfortran\clf\long_string>string3
S1 = 1234567890
S1(1:1) = 1
S1(2:2) = 2
S1(3:3) = 3
S1(4:4) = 4
S1(5:5) = 5
S1(6:6) = 6
S1(7:7) = 7
S1(8:8) = 8
S1(9:9) = 9
S1(10:10) = 0
A2 = 1234567890
A2(1) = 1
A2(2) = 2
A2(3) = 3
A2(4) = 4
A2(5) = 5
A2(6) = 6
A2(7) = 7
A2(8) = 8
A2(9) = 9
A2(10) = 0