problem with iso_c_bindings and fortran strings

Hi there,
I have a problem with iso_c_bindings and fortran character parameters. My simple C function:

#include <stdio.h>

void testc(const char *str) {
 printf("fstring : <%s>\n",str);
}

and the fortran test program:

program test
  use iso_c_binding
 interface
  subroutine testc(name) bind(c)
   use iso_c_binding
   character(kind=c_char,len=*) :: name
  end subroutine testc
 end interface

 call testc("test 1")
 call testc("test 2")
end program test

When I compile above code with pgf90 8.0-6 I get the following results:

fstring : <test 1test 2>
fstring : <test 2>

Somehow the two parameter strings are not separated. Any suggestions?

Hi Magi,

C uses the null character to mark the end of a string. Fortran does not. Hence, when passing a string from Fortran to C, you need to add a null character. For example:

% cat test1.f90
program test
  use iso_c_binding
 interface
  subroutine testc(name) bind(c)
   use iso_c_binding
   character(kind=c_char,len=*) :: name
  end subroutine testc
 end interface

 call testc("test 1"//char(0))
 call testc("test 2"//char(0))
end program test

% pgf90 test1.f90 test1_c.o
test1.f90:
% a.out
fstring : <test 1>
fstring : <test 2>

Hope this helps,
Mat