Fortran pass character * 81 array for c / C ++ code

I'm fresh in programming, I want to call the fortran function in my C ++ code. The thing is, I don’t know how to pass the fortran * 81 array to my C ++.

fortran code is as follows:

subroutine func01(a)
    implicit none
    character*81 a(2)
    write(*,*) a(1)
    write(*,*) a(2)
end

C ++ code is similar:

#include <iostream>

extern "C"{
    void func01_( const char **a );
}    

int main()
{
    const char *a[2];
    a[0]="Hello world!";
    a[1]="This is a test!";
    func01_(a);
    return 0;
}

I base tested my fortran code using this

program pro01
    character*81 a(2)
    a(1)='Hello world!'
    a(2)='This is a test!'
    call func01(a)
end program pro01

'func01 (a)' works well.

thanks @PaulMcKenzie, I fixed some problems with the fool .....

However, when I compiled the cpp code, the result was like dirty codes like:

 7
@L
@  n  @ UH j  FP
 @  n   U շ =  U ྼ      @  

what should I do?

0
source share
2 answers

, , gcc4 Linux (x86_64), , . ( , C- Fortran .)

func01.f90

subroutine func01( a )
    character(*) :: a( 2 )
    print *
    print *, "char length = ", len(a(1)), len(a(2))
    print *, "raw a(1) : [", a(1), "]"
    print *, "raw a(2) : [", a(2), "]"
    print *, "trim     : [", trim(a(1)), "] [", trim(a(2)), "]"
end

main.cpp

extern "C" {
    void func01_( char *c, const int len );
}

#include <iostream>
#include <cstring>  // for memset()
int main()
{
    const int lenmax = 30, numstr = 3; // changed char length to 30 to fit in the terminal
    char a[ numstr ][ lenmax ];
    std::string str[ numstr ];

    str[0] = "moon"; str[1] = "mercury"; str[2] = "jupiter";

    for( int k = 0; k < numstr; k++ ) {
        memset( a[k], ' ', lenmax );  // fill space                                              
        str[k].copy( a[k], lenmax );  // copy at most lenmax char (no \0 attached)                        
    }

    func01_( a[0], lenmax );
    func01_( a[1], lenmax ); // pass from mercury
    return 0;
}

$ g++ func01.f90 main.cpp -lgfortran

char length =           30          30
raw a(1) : [moon                          ]
raw a(2) : [mercury                       ]
trim     : [moon] [mercury]

char length =           30          30
raw a(1) : [mercury                       ]
raw a(2) : [jupiter                       ]
trim     : [mercury] [jupiter]
+2

C Fortran.

++, :

#include <iostream>

extern "C" void func01(const char **a);

int main()
{
  const char *a[2] = {"Hello World","This is a test"};
  func01(a);
  return 0;
}

, , Fortran. C, Fortran 2003. Fortran func01 :

subroutine func01(cstrings) bind(C,name="func01")
  use, intrinsic :: iso_c_binding, only: c_ptr, c_char, c_f_pointer
  implicit none
  type(c_ptr), dimension(2), target, intent(in) :: cstrings
  character(kind=c_char), pointer :: a1(:), a2(:)

  ! size_t strlen(char * s);
  interface
     function strlen(s) bind(C, name='strlen')
       use, intrinsic :: iso_c_binding, only: c_ptr, c_size_t
       implicit none
       type(c_ptr), intent(in), value :: s
       integer(c_size_t) :: strlen
     end function strlen
  end interface

  call c_f_pointer(cstrings(1), a1, [strlen(cstrings(1))])
  call c_f_pointer(cstrings(2), a2, [strlen(cstrings(2))])
  write (*,*) a1
  write (*,*) a2
end subroutine func01

bind - , C , C . cstrings 2 C, *[2] **. - , C strlen, c_f_pointer, Fortran.

, , :

$ ./string-array-test
 Hello World
 This is a test

gcc 5.1.0.

+2

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


All Articles