Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > Call Array valu...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 11 Topic 8155 of 8528
Post > Topic >>

Call Array valued Fortran function from C

by koentjepoppe@[EMAIL PROTECTED] Apr 19, 2008 at 10:33 AM

Dear all,

I have a problem that can be reduced to the following situation:

- mainf.f90 (see below)
       a Fortran function that returns an array containing
       n-copy's of a given double precision number.
- mainc.c (see below)
       a C-file that would like to use this function.

The problem is that it doesn't work (Bus Error on assigment of
array(I) in the Fortran code). One way or another, the output argument
is not available in the Fortran code.

I know one solution would be to change the function into a subroutine,
adding the result as an extra argument, but this would spoil the whole
design of the project. So I'd like to know if anyone has an
alternative way to solve this.

Best Regards

ps: for the sake of completeness: I'm using the latest stable
gFortran, gcc 4.3 and I'm working on a Intel Mac.

[1]: http://docs.sun.com/source/806-3593/11_cfort.html


---8<---- MAINF.F90 ---8<-------8<-------8<----
function repeatdouble( N, d ) result( array )
    integer, intent( in ) :: N
    double precision, dimension( N ) :: array
    double precision, intent( in ) :: d

    print *, "repeat ", N, " times ..."
    print *, "double ", d, "."
    print *, "array :", array
    do I = 1,N
        print *, "making copy nb", i, " of ", N
        array(I) = d
    end do
end function repeatdouble
--->8------->8------->8------->8------->8------->8----



---8<---- MAINC.C ---8<-------8<-------8<----
#include <stdio.h>
extern void repeatdouble_( double*, int*, double* );
int main(){

	double buf[9] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
	double* ptr2buf = buf;
	double d = 3.141592654;
	int  n = 4;
	int i;

	// make n copies of ch in sbf
	repeatdouble_( ptr2buf, &n, &d );

	for( i=0; i<9; i++){
		printf("buf[%i]=%d\n", i, buf[i]);
	}
}
--->8------->8------->8------->8------->8------->8----
 




 11 Posts in Topic:
Call Array valued Fortran function from C
koentjepoppe@[EMAIL PROTE  2008-04-19 10:33:39 
Re: Call Array valued Fortran function from C
glen herrmannsfeldt <g  2008-04-19 12:11:20 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-19 15:54:39 
Re: Call Array valued Fortran function from C
"FX" <couder  2008-04-19 22:23:33 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-19 17:14:50 
Re: Call Array valued Fortran function from C
"FX" <couder  2008-04-20 10:49:38 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-20 09:26:43 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-20 21:21:14 
Re: Call Array valued Fortran function from C
Sebastian Hanigk <hani  2008-04-20 09:44:34 
Re: Call Array valued Fortran function from C
nf.editor@[EMAIL PROTECTE  2008-04-20 08:56:10 
Re: Call Array valued Fortran function from C
"FX" <couder  2008-04-20 16:11:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 18 20:45:18 CDT 2008.