title: R calling Fortran subroutine with character argument
body: <p>How can I call from R a Fortran subroutine with a character argument? My attempt to do so does not work, although I am able to call Fortran subroutines with double precision arguments. For the Fortran code</p>
  
  <pre><code>subroutine square(x,x2)
    double precision, intent(in)  :: x
    double precision, intent(out) :: x2
    x2 = x*x
  end subroutine square
  
  subroutine pow(c,x,y)
    character (len=255), intent(in) :: c
    double precision, intent(in)    :: x
    double precision, intent(out)   :: y
    if (c == "s") then
       y = x**2
    else if (c == "c") then
       y = x**3
    else
       y = -999.0d0 ! signals bad argument
    end if
  end subroutine pow
  </code></pre>
  
  <p>and R code</p>
  
  <pre><code>dyn.load("power.dll") # dll created with gfortran -shared -fPIC -o power.dll power.f90
  x &lt;- 3.0
  foo &lt;- .C("square_",as.double(x),as.double(0.0))
  print(foo)
  bar &lt;- .C("pow_",as.character("c"),as.double(x),as.double(0.0))
  print(bar)
  </code></pre>
  
  <p>The output from </p>
  
  <pre><code>C:\programs\R\R-3.6.1\bin\x64\rterm.exe --vanilla --slave &lt; xcall_power.r
  </code></pre>
  
  <p>is </p>
  
  <pre><code>[[1]]
  [1] 3
  
  [[2]]
  [1] 9
  
  [[1]]
  [1] "c"
  
  [[2]]
  [1] 3
  
  [[3]]
  [1] -999
  </code></pre>