Re: fortran subroutine problem

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Tue, 28 Mar 2006 21:52:58 -0700 (MST)

Hello,

Passing strings/character variables between fortran
and C is not standardized. It is OS/compiler dependent.

(This may be addressed by the fortran 2003 standard
  which does include some standard way of interfacing
  C with fortran.)

It is possible to pass *one* string from NCL to fortran
and *one* fortran character variable to NCL.
It is *not* possible to pass arrays because it is not standardized.

;------------------------------> NCL code
external DEMO "./csdemo.so"
begin
     cstring = new (8, character) ; returned from fortran
     ncl2f = "NCL->F" ; passed to fortran

     DEMO::csdemo(ncl2f , cstring)

     stringc = chartostring( cstring )
     print ( stringc )
end

c------------------------------> fortran code
C NCLFORTSTART
       subroutine csdemo (string_in, string_out)
       character*(*) string_in ! passed FROM ncl
       character*8 string_out ! passed TO ncl
C NCLEND
       print *, string_in

       string_out = "F-to-NCL"
       return
       end

+++++++++++++++++++++++++++++++++++

Give the above a try!! :-)

+++++++++++++++++++++++++++++++++
The following is untested but if you know that [say] 3
strings will be passed back ....
+++++++++++++++++++++++++++++++++
C NCLFORTSTART
       subroutine jerry (str1, str2, str3)
       character*8 str1 ! passed TO ncl
       character*22 str2 ! passed TO ncl
       character*10 str3 ! passed TO ncl
C NCLEND
       str1 ="foo_1"
       str2 ="foofoofoofoo_2"
       str3 ="foofoo3"

       return
       end

begin
     cstr1 = new ( 8, character) ; returned from fortran
     cstr2 = new (22, character) ; returned from fortran
     cstr3 = new (10, character) ; returned from fortran

     DEMO::csdemo(cstr1,cstr2,cstr3)

     str1 = chartostring( cstr1 )
     print ( str1 )
     str2 = chartostring( cstr2 )
     print ( str2 )
     str3 = chartostring( cstr3 )
     print ( str3 )
end

good luck
D

On Wed, 29 Mar 2006, jerry wrote:

> Hi
>
> i need to call the fortran subroutine and wish it could return
> a one dimension array of type string .
> the code like below :
>
> ncl code : (test.ncl)
> external EX01 "./ex01.so"
> begin
>
> var = new(3,string)
> EX01::getstr(var)
> print(var)
>
> end
>
> fortran code : (a.f)
> subroutine getstr(s)
> character*3 s(3)
> s(1) = "aaa"
> s(2) = "bbb"
> s(3) = "ccc"
>
> end
>
> ex01.wib code
> C NCLFORTSTART
> subroutine getstr(s)
> character*3 s(3)
> C NCLEND
>
> when i run the ncl scripts it will show the error message :
> fatal : Argument type mismatch on argument (0) of (getstr) can not coerce
> fatal : Execute : Error occurred at or near line 9 in file test.ncl
>
> Thank you for help!!
_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Mar 28 2006 - 21:52:58 MST

This archive was generated by hypermail 2.2.0 : Thu Mar 30 2006 - 15:04:24 MST