title: passing char arrays from c++ to fortran
body: <p>I am having trouble passing char arrays from c++ to fortran (f90).</p>
  
  <p>Here is my c++ file, 'cmain.cxx':</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  extern "C" int ftest_( char (*string)[4] );
  
  int main() {
      char string[2][4];
  
      strcpy(string[0],"abc");
      strcpy(string[1],"xyz");
  
      cout &lt;&lt; "c++: string[0] = '" &lt;&lt; string[0] &lt;&lt; "'" &lt;&lt; endl;
      cout &lt;&lt; "c++: string[1] = '" &lt;&lt; string[1] &lt;&lt; "'" &lt;&lt; endl;
  
      ftest_(string);
  
      return 0;
  }
  </code></pre>
  
  <p>Here is my fortran file, 'ftest.f90':</p>
  
  <pre><code>SUBROUTINE FTEST(string)
  
  CHARACTER*3 string(2)
  CHARACTER*3 expected(2)
  data expected(1)/'abc'/
  data expected(2)/'xyz'/
  
  DO i=1,2
      WRITE(6,10) i,string(i)
  10  FORMAT("fortran: string(",i1,") = '", a, "'" )
  
      IF(string(i).eq.expected(i)) THEN
          WRITE(6,20) string(i),expected(i)
  20      FORMAT("'",a,"' equals '",a,"'")
      ELSE
          WRITE(6,30) string(i),expected(i)
  30      FORMAT("'",a,"' does not equal '",a,"'")
      END IF
  ENDDO
  
  RETURN
  END
  </code></pre>
  
  <p>The build process is:</p>
  
  <pre><code>gfortran -c -m64   ftest.f90 
  g++ -c  cmain.cxx
  gfortran -m64 -lstdc++ -gnofor_main -o test ftest.o cmain.o
  </code></pre>
  
  <p>Edit: note that the executable can also be build via:</p>
  
  <pre><code>g++ -lgfortran -o test ftest.o cmain.o
  </code></pre>
  
  <p>Also, the -m64 flag is required as I am running OSX 10.6.</p>
  
  <p>The output from executing 'test' is:</p>
  
  <pre><code>c++: string[0] = 'abc'
  c++: string[1] = 'xyz'
  fortran: string(1) = 'abc'
  'abc' equals 'abc'
  fortran: string(2) = 'xy'
  'xy' does not equal 'xyz'
  </code></pre>
  
  <p>Declaring the 'string' and 'expected' character arrays in ftest.f90 with size 4, ie:</p>
  
  <pre><code>CHARACTER*4 string(2)
  CHARACTER*4 expected(2)
  </code></pre>
  
  <p>and recompiling gives the following output:</p>
  
  <pre><code>c++: string[0] = 'abc'
  c++: string[1] = 'xyz'
  fortran: string(1) = 'abc'
  'abc' does not equal 'abc '
  fortran: string(2) = 'xyz'
  'xyz' does not equal 'xyz '
  </code></pre>
  
  <p>Declaring the character arrays in 'cmain.cxx' with size 3, ie:</p>
  
  <pre><code>extern "C" int ftest_( char (*string)[3] );
  
  int main() {
      char string[2][3];
  </code></pre>
  
  <p>and reverting to the original size in the fortran file (3), ie:</p>
  
  <pre><code>CHARACTER*3 string(2)
  CHARACTER*3 expected(2)
  </code></pre>
  
  <p>and recompiling gives the following output:</p>
  
  <pre><code>c++: string[0] = 'abcxyz'
  c++: string[1] = 'xyz'
  fortran: string(1) = 'abc'
  'abc' equals 'abc'
  fortran: string(2) = 'xyz'
  'xyz' equals 'xyz'
  </code></pre>
  
  <p>So the last case is the only one that works, but here I have assigned 3 characters to a char array of size 3 which means the terminating '\0' is missing, and leads to the 'abcxyz' output - this is not acceptable for my intended application.  </p>
  
  <p>Any help would be greatly appreciated, this is driving me nuts!</p>