我希望能够将一个字符串数组从c ++传递给fortran90。我可以使用以下fortran模块传递双数组和字符数组:
MODULE rocker
CONTAINS
SUBROUTINE alltwice(numbers, doubles, n)
INTEGER*4, INTENT(IN) :: n
REAL(KIND=dp), INTENT(IN) :: numbers(1:n)
REAL(KIND=dp), INTENT(OUT) :: doubles(1:n)
DO i=1,n
WRITE(*,*) numbers(i)
END DO
WRITE(*,*)
DO i=1,n
doubles(i)=2.0*numbers(i)
END DO
END SUBROUTINE alltwice
SUBROUTINE printstring(str, k)
CHARACTER(len=k), INTENT(IN) :: str
WRITE(*,*) str
END SUBROUTINE printstring
SUBROUTINE printsevstrings(strs, ks, n)
INTEGER, INTENT(IN) :: n, ks
CHARACTER(len=ks), INTENT(IN) :: strs(1:n)
WRITE(*,*) (strs(i), i=1, n)
END SUBROUTINE printsevstrings
END MODULE rocker
在c ++代码中我有
extern "C"{
extern void __rocker_MOD_alltwice(double a1[], double a2[], const int& n);
extern void __rocker_MOD_printstring(const char* c, int& length);
extern void __rocker_MOD_printsevstrings(const char* argv[], int& length, int& number);
}
现在我可以使用例如传递双数组。
const int n=4;
double a1[n], a2[n];
for(int i=0;i<n;i++) a1[i]=i+0.5;
__rocker_MOD_alltwice(a1, a2, n);
for(int i=0;i<n;i++) {cout << i << " " << a2[i] << endl;}
和使用例如
的字符数组string s="jkljkloi9";
const char* c=s.c_str();
int length=s.length();
__rocker_MOD_printstring(c, length);
但我还没能做出像这样的工作:
int main(int argc, const char* argv[]) {
length=100;
const char* passarg[argc];
for(int i=0;i<argc;i++) passarg[i]=argv[i];
__rocker_MOD_printsevstrings(passarg, length, argc);
}