The correct way to pass a string from Fortran to C / C ++

I want to pass a string from Fortran to C / C ++. Here is my Fortran code:

subroutine zdplaskinGetSpeciesName(cstring, index) bind(C, name='zdplaskinGetSpeciesName')
  use iso_c_binding
  use ZDPlasKin
  implicit none
  integer, intent(in) :: index
  CHARACTER(10), TARGET :: fstring = ''
  TYPE(C_PTR) :: cstring
  fstring = species_name(index+1)
  cstring = c_loc(fstring)
end subroutine zdplaskinGetSpeciesName

ZDPlasKinis a module that has species_name(i).

extern "C" void zdplaskinGetSpeciesName(char* cstring[], size_t* index);
char* cstring[1];
size_t index = 1;
zdplaskinGetSpeciesName(cstring, &index);
string speciesName = string(cstring[0]);
cout << speciesName << endl;

The result for this method seems perfect. However, I want to trim the final space ( character(10), which gives extra space), so my C ++ code can read the line correctly. I tried a different way.

subroutine zdplaskinGetSpeciesName(cstring, index) bind(C, name='zdplaskinGetSpeciesName')
  use iso_c_binding
  use ZDPlasKin
  implicit none
  integer, intent(in) :: index
  CHARACTER(:), allocatable, TARGET :: fstring
  TYPE(C_PTR) :: cstring
  fstring = trim(species_name(index+1))
  cstring = c_loc(fstring)
end subroutine zdplaskinGetSpeciesName

But this way I got some weird characters.


, . - , . , , . , , , . , StackOverflow ( ++ Fortran. qaru.site/questions/1682328/...)

, ? . , ?

+4
2

. , , , .

C . , . , , .

, fstring . .

,

subroutine zdplaskinGetSpeciesName(cstring, index) bind(C, name='zdplaskinGetSpeciesName')
  use iso_c_binding
  use ZDPlasKin
  implicit none
  integer, intent(in) :: index
  CHARACTER(11), POINTER :: fstring
  TYPE(C_PTR) :: cstring
  allocate(fstring)
  fstring = species_name(index+1)
  fstring(11:11) = c_null_char
  cstring = c_loc(fstring)
end subroutine zdplaskinGetSpeciesName

, char

   integer :: len
   ...
   len = len_trim(fstring)
   fstring(len+1:len+1) = c_null_char

, + 1 (character(:)), . 1 .

. , Fortran! .


++ Fortran. . ++, Fortran .

:

subroutine zdplaskinGetSpeciesName(cstring, index) bind(C, name='zdplaskinGetSpeciesName')
  use iso_c_binding
  use ZDPlasKin
  implicit none
  integer, intent(in) :: index
  TYPE(C_PTR), intent(in) :: cstring
  CHARACTER(kind=C_CHAR), POINTER :: fstring(:)
  integer :: i, c_len, name_len

  c_len = c_strlen(cstring)
  c_f_pointer(cstring, fstring, [c_len])

  associate(name => species_name(index+1))
    name_len = trim_len(name)
    do i = 1, name_len
      fstring(i) = name(i:i)
    end do
    fstring(name_len+1:name_len+1)
  end do
end subroutine zdplaskinGetSpeciesName

. ++. c_strlen http://fortranwiki.org/fortran/show/c_interface_module


100%, character(kind=c_char) , , , .

+4

- . http://fortranwiki.org/fortran/show/c_interface_module. F_C_string_ptr.

subroutine zdplaskinGetSpeciesName(cstring, index) bind(C, name='zdplaskinGetSpeciesName')
  use ZDPlasKin
  use C_interface_module
  implicit none
  integer, intent(in) :: index
  TYPE(C_PTR), intent(in) :: cstring
  character(:), allocatable :: fstring
  fstring = trim(species_name(index+1))
  call F_C_string_ptr(fstring, cstring)
end subroutine zdplaskinGetSpeciesName

c ++-:

extern "C" void zdplaskinGetSpeciesName(char* cstring[], size_t* index);
for (size_t i = 0; i < zdplaskinNSpecies(); i++) {
    char* cstring[10];
    zdplaskinGetSpeciesName(cstring, &i);
    printf("%s\n",*cstring);
    string speciesName(*cstring);
    cout << speciesName << endl;
}
0

Source: https://habr.com/ru/post/1682326/


All Articles