Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing a string as an argument when the dummy has specified length

if I have this code

module test
contains
   subroutine xx(name)
      character(len=20), intent(in), optional :: name

      if (present(name)) then
         print *, name
      else
         print *, "foo"
      endif
   end subroutine
end module
program x
   use test

   call xx()
   call xx("foo2")
end program

It will not compile since "foo2" is not of length 20, and the compiler complains

test.f90(17): error #7938: Character length argument mismatch.   ['foo2']
   call xx("foo2")
-----------^

How can I make this thing work, without modifying the subroutine dummy len specification ? Is it mandatory to have an intermediate variable declared with the same length and pass that at call time ?

like image 298
Stefano Borini Avatar asked Jan 24 '11 08:01

Stefano Borini


2 Answers

Standard language can be hard to understand. I read the clause cited by @kemiisto as requiring length (dummy arg) <= length (actual arg). Here length (dummy arg) = 20 and length (actual arg) = 4, so length (dummy arg) > length (actual arg), which is not allowed by the clause. The clause talks about truncating the actual to match the dummy, if needed, not padding it with blanks.

The code will work if you replace character(len = 20) with character(len = *) Is there a problem with this since you didn't want to modify the length of the dummy argument specification?

like image 170
M. S. B. Avatar answered Nov 13 '22 19:11

M. S. B.


Seems to me like standard compliant behavior.

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.

However gfortran just rise a warning message

Warning: Character length of actual argument shorter than of dummy argument 'name' (4/20) at (1)

It seems that Intel Fortran compiler is more standard compliant in that respect. So intermediate variable is probably the only option.

Or actually just declaring a variable is option because you do not have one in your code. You have literal constant. Hard-coded values are not a good idea anyway. =)

like image 2
Wildcat Avatar answered Nov 13 '22 17:11

Wildcat