title: Pass arrays from C/C++ to Fortran and return a calculated array
body: <p>I am trying to pass an array from C/C++ into a Fortran 2003 module and get the calculated values back into C/C++. I've been able to pass and return single values (scalars) just fine, but getting an <em>array</em> back and forth is proving difficult. I've found many threads on scalar values and I've been successful at making those work.</p>
  
  <p>I've modeled my array based functions after my working scalar functions.</p>
  
  <p>I am using gcc/gfortran.</p>
  
  <p>Here's the Fortran module (ConvertUnitsLib.f03).</p>
  
  <pre><code>module ConvertUnitsLib
  
  use :: iso_c_binding ! for C/C++ interop
  real(c_double), bind(c) :: degF, degC
  
  public DegCtoF
  
  contains
  
  !
  ! Convert temperature degrees Celsius Fahrenheit
  !
  real(kind = c_double) function DegCtoF(degC) result(degF) &amp;
      &amp; bind(c, name = "DegCtoF")
  
      real(c_double), intent(in), dimension(:) :: degC
      real(c_double), dimension(size(degC)) :: degF
  
      do i = 1, size(degC)
          degF(i) = ( degC(i) * 1.8 ) + 32
      end do
  
  end function DegCtoF
  
  
  ! End of module
  end module ConvertUnitsLib
  </code></pre>
  
  <p>And the C/C++, (CFort.cpp)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #ifdef __cplusplus
  extern"C" {
  #endif
      double DegCtoF(double *[]);
  #ifdef __cplusplus
  }
  #endif
  
  
  /**********************************************************************/
  
  
  int main(int argc, char *argv[])
  {
      printf("C/C++ and Fortran together!\n");
  
      double DegreesC[2] = {32, 64};
      double DegreesF[2];
  
      DegreesF = DegCtoF(&amp;DegreesC);
      printf("%3.1f [C] = %3.1f [F]\n", DegreesC, DegreesF );
  
      return 0;
  }
  </code></pre>
  
  <p>And last but not least, the Makefile</p>
  
  <pre><code># C++ directives
  CC=g++
  CFLAGS=-std=c++11
  
  # Fortran directives
  FC=gfortran
  FFLAGS=-std=f2003
  
  all: clean
      $(FC) $(FFLAGS) -c -fcheck=all ConvertUnitsLib.f03
      $(CC) $(CFLAGS) -c CFort.cpp
      $(FC) $(FFLAGS) ConvertUnitsLib.o CFort.o -o convert
  
  clean:
      rm -f *.o
      rm -f *.mod
  </code></pre>