davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Jun 15, 2011 12:57 pm Post subject: |
|
|
In one of my Fortran books it says that when a CHARACTER dummy argument is declared to have a specific length, the actual argument must be the same length or longer. If it is longer, the leading characters are passed up to the specified length.
See the code below (which compiles and runs OK).
If this is the correct behaviour, then it possibly provides a "consistency" argument for the behaviour seen with ICHAR (though ICHAR is an intrinsic and different rules might apply). But I don't know if there's a general rule about this in the standard for Fortran 95, or if its just a F2003 extension that is supported -- if there is ICHAR could be off the hook.
This is in the Fortran 2003 standard:
"Fortran 2003, 12.4.1.2 Actual arguments associated with dummy data objects
If a scalar dummy argument is of type default character, 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."
Code: |
module foo
contains
function first(c)
character(len=1), intent (in) :: c !< Means actual argument has len >= 1
character(len=1) :: first
first = c
end function
end module foo
program anon
use foo
implicit none
character(len=10) :: str
str = 'ABCDEFGHIJ'
print '("First character of ",a," is ",a)', str, first(str)
end program anon
|
|
|