From: Isomorphismus on
Hi everybody,
I try to pass a Fortran array to a c++-function, assuming that the
name of the fortran array is a pointer, indicating the first element.
it looks like this:

the fortran file:

program arrayfc

implicit none

integer,parameter :: n=5
integer :: stopp,SUMME
real :: a(n),sum,mw,MITTELWERT,wert
wert=1
a=wert

sum=SUMME(a,n)
mw=MITTELWERT(a,n)

write(*,*) "Die Summe betraegt ",sum," ,der Mittelwert ",mw
read(*,*) stopp

end program arrayfc

and the c++-file:
#include <iostream>
using namespace std;

extern "C" {double _stdcall SUMME(double*,int*);
double _stdcall MITTELWERT(double*,int*);
}

double _stdcall SUMME(double*a,int*n) { int i=0;
for(i=0;i<(*n)+1;i++) cout<<a[i];
double sum=0;
for(i=0;i<(*n)+1;i++) sum=sum+a[i];
return sum;
}

double _stdcall MITTELWERT(double*a,int*n){int i=0;
double sum=0;
for(i=0;i<(*n)+1;i++) sum=sum+a[i];
return sum/(*n);
}

in the first c++-function, I look if the routine really got the array
I typed in, but that´s not the case, I get - something undefined when
I give out the a(i), so in the passing something goes wrong - but what
does? is that a not a pointer?

Thanks for any help,
Iso
From: Gordon Sande on
On 2008-02-02 11:50:52 -0400, Isomorphismus <nataliehapp(a)hotmail.com> said:

> Hi everybody,
> I try to pass a Fortran array to a c++-function, assuming that the
> name of the fortran array is a pointer, indicating the first element.
> it looks like this:
>
> the fortran file:
>
> program arrayfc
>
> implicit none
>
> integer,parameter :: n=5
> integer :: stopp,SUMME
> real :: a(n),sum,mw,MITTELWERT,wert
> wert=1
> a=wert
>
> sum=SUMME(a,n)
> mw=MITTELWERT(a,n)
>
> write(*,*) "Die Summe betraegt ",sum," ,der Mittelwert ",mw
> read(*,*) stopp
>
> end program arrayfc
>
> and the c++-file:
> #include <iostream>
> using namespace std;
>
> extern "C" {double _stdcall SUMME(double*,int*);
> double _stdcall MITTELWERT(double*,int*);
> }
>
> double _stdcall SUMME(double*a,int*n) { int i=0;
> for(i=0;i<(*n)+1;i++) cout<<a[i];
> double sum=0;
> for(i=0;i<(*n)+1;i++) sum=sum+a[i];
> return sum;
> }
>
> double _stdcall MITTELWERT(double*a,int*n){int i=0;
> double sum=0;
> for(i=0;i<(*n)+1;i++) sum=sum+a[i];
> return sum/(*n);
> }
>
> in the first c++-function, I look if the routine really got the array
> I typed in, but that�s not the case, I get - something undefined when
> I give out the a(i), so in the passing something goes wrong - but what
> does? is that a not a pointer?
>
> Thanks for any help,
> Iso

Whatever compiler/system you are using will surely have a long section
in its manual on mixed language programming. It will not be the same as
other compilers. Since you forgot to mention the compiler it is up to
you to RTFM.

Fortran often uses machine level pointers internally but that is far
from guaranteed. Can you say descriptor? That is why there is a mixed
language in the manual.