title: Passing an array of C-strings to Fortran (iso_c_binding)
body: <p>How can I pass an array of C strings (<code>char* cstrings[]</code>) to a Fortran
  subroutine?</p>
  
  <p>Question <a href="https://stackoverflow.com/questions/9686532" title="Arrays of strings in fortran-C bridges using iso_c_binding">Arrays of strings in fortran-C bridges using iso_c_binding</a> is definitely related, but the answer does not seem correct and does not even compile with GNU Fortran.</p>
  
  <p>I am currently working on a C interface for a Fortran code, and I expected that <code>iso_c_binding</code> (which I had used previously) would make this a piece of cake.  No luck so far for arrays of C strings...</p>
  
  <p>The Fortran subroutine should take an array of strings as an argument.
  In plain Fortran I would write something like the following:</p>
  
  <pre><code>subroutine print_fstring_array(fstring)
  
    implicit none
  
    character(len=*), dimension(:), intent(in) :: fstring
    integer                                    :: i
  
    do i = 1, size(fstring)
      write(*,*) trim(fstring(i))
    end do
  
  end subroutine print_fstring_array
  </code></pre>
  
  <p>One way to pass a single C string to Fortran is as C pointer (<code>c_ptr</code>) (I know, I could also use an array of <code>character(kind=c_char)</code>)</p>
  
  <pre><code>subroutine print_cstring(cstring) bind(C)
  
    use iso_c_binding, only: c_ptr, c_f_pointer, c_loc, c_null_char
    implicit none
  
    type(c_ptr), target, intent(in) :: cstring
    character(len=1024), pointer    :: fstring
    integer                         :: slen
  
    call c_f_pointer(c_loc(cstring), fstring)
    slen = index(fstring, c_null_char) - 1
    write(*,*) fstring(1:slen)
  
  end subroutine print_cstring
  </code></pre>
  
  <p>So, I assumed an array of <code>c_ptr</code> would be a good idea</p>
  
  <pre><code>subroutine print_cstring_array(n, cstring) bind(C)
  
    use iso_c_binding, only: c_ptr, c_int, c_f_pointer, c_loc, c_null_char
    implicit none
  
    integer(kind=c_int),               intent(in) :: n
    type(c_ptr), dimension(n), target, intent(in) :: cstring
    character(len=1024), pointer                  :: fstr
    integer                                       :: slen, i
  
    do i = 1, n
      call c_f_pointer(c_loc(cstring(i)), fstring)
      slen = index(fstring, c_null_char) - 1
      write(*,*) fstring(1:slen)
    end do
  
  end subroutine print_cstring_array
  </code></pre>
  
  <p>but this produces a segmentation fault.  </p>
  
  <p>The C code for the last example is</p>
  
  <pre><code># include "stdio.h"
  # include "fstring.h"
  
  void main(void) {
    char* cstring[] = { "abc", "def", "ghi", "jkl" };
    int n = 4;
    print_cstring_array(&amp;n, cstring);
  }
  </code></pre>
  
  <p>and the contents of the header file <code>fstring.h</code> are simply:</p>
  
  <pre><code>void print_cstring_array(int* n, char* cstring[]);
  </code></pre>
  
  <p>I am targeting GNU Fortran and Intel Fortran and have tested the above with GNU Fortran.  The <strong>lengths of the strings is fixed</strong> (3 in the above example), in case this simplifies the solution.  However, the dimension of the array can vary.</p>
  
  <p>Any pointers (even C pointers) would be greatly appreciated.</p>