Zero based fortran strings

I don’t think the copy should be necessary.

character(*), parameter :: c = 'Hello World'
   call sub(0,len(c)-1,c)
contains
   subroutine sub(first,last,a)
      integer, intent(in) :: first, last
      character, intent(in) :: a(first:last)
      write(*,'(2(i0,1x),*(a))') first, last, a(first:last)
   end subroutine sub
end program

It is standard to associate a character string to a character array, and I think all fortran compilers will establish that argument association without copy-in/copy-out. This example uses an internal subprogram, but I think the same thing can be done now inline with an ASSOCIATE block.

[edit] I looked at the description of ASSOCIATE in MFE, and maybe this is not possible after all. ASSOCIATE blocks have most of the functionality of dummy argument association, but this feature of character strings and arrays seems to be not allowed. Anyone know for certain whether or not this is possible?

2 Likes