HP Fortran Programmer's Reference (September 2007)

Data types and data objects
Intrinsic data types
Chapter 5 121
For information about substring operations on an array of strings, see “Array sections” on
page 68.
Character strings as automatic data objects
An automatic data object can be either an automatic array (see “Explicit-shape arrays” on
page 59) or a character string that is local to a subprogram and whose size is nonconstant.
The size of a character string is determined when the subprogram is called and can vary from
call to call.
An automatic character string must not be:
A dummy argument
Declared with the SAVE attribute
Initialized in a type declaration statement or DATA statement
The following example, swap_names.f90, illustrates the use of automatic character strings:
Example 5-2 swap_names.f90
PROGRAM main
! actual arguments to pass to swap_names
CHARACTER(6) :: n1 = "George", n2 = "Martha"
CHARACTER(4) :: n3 = "pork", n4 = "salt"
PRINT *, "Before: n1 = “, n1, " n2 = “, n2
CALL swap_names(n1, n2)
PRINT *, "After: n1 = “, n1, " n2 = “, n2
PRINT *, "Before: n3 = “, n3, " n4 = “, n4
CALL swap_names(n3, n4)
PRINT *, "After: n3 = “, n3, " n4 = “, n4
END PROGRAM main
! swap the arguments - two character strings of the same length
SUBROUTINE swap_names (name1, name2)
CHARACTER(*) :: name1, name2 ! the arguments
! declare another character string, temp, to be used in the
! exchange. temp is an automatic data object, its length
! can vary from call to call
CHARACTER(LEN(name1)) :: temp
! the exchange
temp = name1
name1 = name2
name2 = temp
END SUBROUTINE swap_names