Fortran subroutine characters args and their lengths and C 
Author Message
 Fortran subroutine characters args and their lengths and C

I was wondering if somebody can clarify argument calling of fortran
subroutines from C to me.

If a certain Fortran subroutine expects a few character arguments, and
if it is to be called from C/C++, does the Fortran routine expect the
lengh of the character array to be passed right after the character
pointer arg? e.g.
If Fortran subroutine is:
   subroutine ForSR(astr, bstr)
   character astr*1, bstr*2
   ....

Then, to call this routine from a C program, should one use:
forsr_(char *cpA, int *iA, char* cpB, int* iB)

where cpA="Q", cpB="XY", iA=1, iB=2?

Or should the length args (iA and iB) be passed after all other args to
the Fortran routine? Or are the length arguments not needed at all?

Thanks,
->HS
PS:
Background: I am trying to understand how to call the ARPACK routine
dsaupd which in dsaupd.f is defined to be
      subroutine dsaupd
      &   ( ido, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam,
      &     ipntr, workd, workl, lworkl, info )
       character  bmat*1, which*2
       integer    ido, info, ldv, lworkl, n, ncv, nev
       Double precision
      &           tol
       integer    iparam(11), ipntr(11)
       Double precision
      &           resid(n), v(ldv,ncv), workd(3*n), workl(lworkl)

       Double precision
      &           one, zero
       parameter (one = 1.0D+0 , zero = 0.0D+0 )

--
(Remove all underscores,if any, from my email address to get the correct
one. Apologies for the inconvenience but this is to reduce spam.)



Sun, 23 Sep 2007 07:35:14 GMT  
 Fortran subroutine characters args and their lengths and C
Different implementations do different things.  Most implementations
of Fortran on UNIX pass the lengths of character arguments at the end
of the argument list.  The C prototype for your routine ForSR would be

      extern void forsr_(char *astr, char *bstr, int len_astr, int
len_bstr);

Most UNIX implementations ignore the lengths if the lengths of the
character dummy arguments in the subroutine are all constants.

 Bob Corbett



Sun, 23 Sep 2007 08:58:56 GMT  
 Fortran subroutine characters args and their lengths and C
[Follow up changed to comp.lang.c - this is OT in both comp.lang.fortran and
sci.math.num-analysis]

Quote:

> I was wondering if somebody can clarify argument calling of Fortran
> subroutines from C to me.

> If a certain Fortran subroutine expects a few character arguments, and
> if it is to be called from C/C++, does the Fortran routine expect the
> lengh of the character array to be passed right after the character
> pointer arg? e.g.
> If Fortran subroutine is:
>    subroutine ForSR(astr, bstr)
>    character astr*1, bstr*2
>    ....

> Then, to call this routine from a C program, should one use:
> forsr_(char *cpA, int *iA, char* cpB, int* iB)

> where cpA="Q", cpB="XY", iA=1, iB=2?

As I explained this at some length the other day in sci.math.num-analysis,
this is OT in comp.lang.fortran. What part of that did you not understand ?

Your wrapper, and the business of passing the arguments to Fortran, are all
related to C/C++. Please ask this question in comp.lang.c (or
comp.lang.c++). You are more likely to get proper answers there.

If you were asking about writing a Fortran wrapper to C, it would be
different. And if I might add, this is even more OT in
sci.math.num-analysis. I have removed that group completely from the list.



Sun, 23 Sep 2007 09:52:29 GMT  
 Fortran subroutine characters args and their lengths and C

Quote:

> I was wondering if somebody can clarify
>  argument [passing to] Fortran [subprograms] from C [for] me.

> If a certain Fortran subroutine expects a few character arguments
> and if it is to be called from C/C++,
>  does the Fortran routine expect the lengh of the character array
> to be passed right after the character pointer arg? e.g.
> If Fortran subroutine is:

>   subroutine ForSR(astr, bstr)
>   character astr*1, bstr*2
>   ....
> Then, to call this routine from a C program, should one use:

>       void forsr_(char *cpA, int *iA, char* cpB, int* iB);

> where cpA="Q", cpB="XY", iA=1, iB=2?

> Or should the length args (iA and iB) be passed after
> all other args to the Fortran routine?

It is implementation dependent
but the typical implementation passes the string lengths *by value*
after all of the other arguments in the order that the strings appear
in the argument list.  To invoke subroutine ForSR in your C program,
you would write:

         void forsr_(char*, char*, int, int);
         forsr_("Q", "XY", strlen("Q"), strlen("XY"));

Quote:
> Or are the length arguments not needed at all?

That depends upon your implementation of subroutine ForSR.

         > cat forsr.f
               subroutine ForSR(astr, bstr)
                 character astr*1, bstr*2
                 if (astr .eq. 'Q') then
                   print *, 'astr = Q'
                   end if
                 if (bstr .eq. 'XY') then
                   print *, 'bstr = XY'
                   end if
                 end subroutine ForSR

         > g77 -Wall -O2 -c forsr.f
         > cat main.c
         void forsr_(const char*, const char*);

         int main(int argc, char* argv[]) {
           forsr_("Q", "XY");
           return 0;
           }

         > gcc -Wall -std=c99 -pedantic -O2 -o main main.c forsr.o -lg2c
         > ./main
          astr = Q
          bstr = XY

Usually, you can get away without passing the string lengths
if the Fortran subroutine doesn't check them
but it's probably safer to pass them.



Sun, 23 Sep 2007 09:04:25 GMT  
 Fortran subroutine characters args and their lengths and C

Quote:

> [Follow up changed to comp.lang.c - this is OT in both comp.lang.fortran and
> sci.math.num-analysis]


>>I was wondering if somebody can clarify argument calling of Fortran
>>subroutines from C to me.

>>If a certain Fortran subroutine expects a few character arguments, and
>>if it is to be called from C/C++, does the Fortran routine expect the
>>lengh of the character array to be passed right after the character
>>pointer arg? e.g.
>>If Fortran subroutine is:
>>   subroutine ForSR(astr, bstr)
>>   character astr*1, bstr*2
>>   ....

>>Then, to call this routine from a C program, should one use:
>>forsr_(char *cpA, int *iA, char* cpB, int* iB)

>>where cpA="Q", cpB="XY", iA=1, iB=2?

> As I explained this at some length the other day in sci.math.num-analysis,
> this is OT in comp.lang.fortran. What part of that did you not understand ?

> Your wrapper, and the business of passing the arguments to Fortran, are all
> related to C/C++. Please ask this question in comp.lang.c (or
> comp.lang.c++). You are more likely to get proper answers there.

> If you were asking about writing a Fortran wrapper to C, it would be
> different. And if I might add, this is even more OT in
> sci.math.num-analysis. I have removed that group completely from the list.

I somewhat disagree that this is entirely off topic in clf.  In order to
know how to call it from C requires both knowledge of Fortran argument
passing and c argument passing.  Either news group is likely to have
someone that knows both.

--

Gary Scott

Fortran Library:  http://www.fortranlib.com

Support the Original G95 Project:  http://www.g95.org
-OR-
Support the GNU GFortran Project:  http://gcc.gnu.org/fortran/index.html

Why are there two?  God only knows.

If electricity comes from electrons, does morality come from morons?



Sun, 23 Sep 2007 09:59:05 GMT  
 Fortran subroutine characters args and their lengths and C

Quote:

> [Follow up changed to comp.lang.c - this is OT in both comp.lang.fortran and
> sci.math.num-analysis]

[A question completely off-topic in comp.lang.c]

Quote:
> As I explained this at some length the other day in sci.math.num-analysis,
> this is OT in comp.lang.fortran. What part of that did you not understand ?

Thank you so much for sending this {*filter*}to comp.lang.c.  It's a good
thing for you want to guard the topicality of sci.math.num-analysis, but
not by posting to other newgroups without regard to their topicality.

Quote:
> Your wrapper, and the business of passing the arguments to Fortran, are all
> related to C/C++.

There is no such thing as C/C++.

In the future, please restrict yourself to comments like your

Quote:
> Please ask this question in comp.lang.c (or
> comp.lang.c++). You are more likely to get proper answers there.

Please do not take it upon yourself to pollute other newsgroups.


Sun, 23 Sep 2007 10:31:32 GMT  
 Fortran subroutine characters args and their lengths and C

Quote:

> As I explained this at some length the other day in sci.math.num-analysis,
> this is OT in comp.lang.fortran.

That's *your* opinion.
I don't agree.

Quote:
> What part of that did you not understand?

Is there anything that I need to explain to you?

Quote:
> Your wrapper, and the business of passing the arguments to Fortran,
> are all related to C/C++.
> Please ask this question in comp.lang.c (or comp.lang.c++).

Good idea.

Quote:
> You are more likely to get proper answers there.

Not likely.

Quote:
> If you were asking about writing a Fortran wrapper to C,
> it would be different.

Nonsense!
It is *much* more important to Fortran programmers than C programmers
for C programmers to be able to call Fortran subprograms from C
because it makes Fortran subprogram libraries accessible
and more valuable to a far larger group of programmers.

Quote:
> And if I might add, this is even more OT in sci.math.num-analysis.

I agree.
Quote:
> I have removed that group completely from the list.



Sun, 23 Sep 2007 10:05:58 GMT  
 Fortran subroutine characters args and their lengths and C
Apparently, _Madhusudan Singh_, on 05/04/05 21:52,typed:

 > [Follow up changed to comp.lang.c - this is OT in both
comp.lang.fortran and
 > sci.math.num-analysis]

 > If you were asking about writing a Fortran wrapper to C, it would be
 > different. And if I might add, this is even more OT in
 > sci.math.num-analysis. I have removed that group completely from the
list.

I disagree regarding s.m.n-a point above. There are numerous queries on
that newsgroup concerning implementation of numerical analysis algorithm
in either C or Fortran or C++. Even a cursory search can clearly show
this. For well known reasons in the field, mixture of C and Fortran or
C++ and Fortran is quite common. So the query was quite on topic in that
group.

I also do not agree with your c.l.c point. But others have already
replied to that effect.

->HS
PS: Can't you resist yourself from trying to police the newsgroups? At
first I read your objections with some attention, but you are overdoing
it now and making unreasonable and nonsensical claims.

--
Please remove the underscores ( the '_' symbols) from my email address
to obtain the correct one. Apologies, but the fudging is to remove spam.



Sun, 23 Sep 2007 11:09:52 GMT  
 Fortran subroutine characters args and their lengths and C

Quote:

> [Follow up changed to comp.lang.c - this is OT in both comp.lang.fortran and
> sci.math.num-analysis]


> > I was wondering if somebody can clarify argument calling of Fortran
> > subroutines from C to me.

> > If a certain Fortran subroutine expects a few character arguments, and
> > if it is to be called from C/C++, does the Fortran routine expect the
> > lengh of the character array to be passed right after the character
> > pointer arg? e.g.
> > If Fortran subroutine is:
> >    subroutine ForSR(astr, bstr)
> >    character astr*1, bstr*2
> >    ....

> > Then, to call this routine from a C program, should one use:
> > forsr_(char *cpA, int *iA, char* cpB, int* iB)

> > where cpA="Q", cpB="XY", iA=1, iB=2?

> As I explained this at some length the other day in sci.math.num-analysis,
> this is OT in comp.lang.fortran. What part of that did you not understand ?

> Your wrapper, and the business of passing the arguments to Fortran, are all
> related to C/C++. Please ask this question in comp.lang.c (or
> comp.lang.c++). You are more likely to get proper answers there.

> If you were asking about writing a Fortran wrapper to C, it would be
> different. And if I might add, this is even more OT in
> sci.math.num-analysis. I have removed that group completely from the list.

Please don't suddenly dump this OT material on c.l.c.  It has
nothing to do with the C language.  The OP probably wants a
newsgroup that deals with his particular system.

Followups returned to you.

--
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on
 "show options" at the top of the article, then click on the
 "Reply" at the bottom of the article headers." - Keith Thompson



Sun, 23 Sep 2007 13:11:37 GMT  
 Fortran subroutine characters args and their lengths and C


Wed, 18 Jun 1902 08:00:00 GMT  
 Fortran subroutine characters args and their lengths and C

Quote:
>Your wrapper, and the business of passing the arguments to Fortran, are all
>related to C/C++. Please ask this question in comp.lang.c (or
>comp.lang.c++). You are more likely to get proper answers there.

clc and clc++ typically are concerned with *standard* C or C++ (respectively)
which really has little to do with implementation details (except as the
implementations either DO or FAIL to follow the standards).  clc and clc++
neither welcome, nor are likely to respond with useful information to posts
which are entirely implementation dependent.  clf, on the other hand, has
many posters who will spend time answering questions which are marginally related
to topic.  Furthermore, as others have indicated, the interoperability of
Fortran with other languages, including C and C++, is considered at least
partially on-topic by most.


Sun, 23 Sep 2007 19:40:37 GMT  
 Fortran subroutine characters args and their lengths and C


Wed, 18 Jun 1902 08:00:00 GMT  
 Fortran subroutine characters args and their lengths and C
Disclaimer: My newfeed is intermittent at the moment, I haven't seen
             the original post, and I may have missed a reply that
             duplicates what follows.  Please excuse or ignore as
             seems appropriate...

Quote:

> [Follow up changed to comp.lang.c - this is OT in both comp.lang.fortran and
> sci.math.num-analysis]

I've dropped comp.lang.c since they're rumored to be not so polite
about this sort of posting...

Quote:

>>I was wondering if somebody can clarify argument calling of Fortran
>>subroutines from C to me.

>>If a certain Fortran subroutine expects a few character arguments, and
>>if it is to be called from C/C++, does the Fortran routine expect the
>>lengh of the character array to be passed right after the character
>>pointer arg? e.g.
>>If Fortran subroutine is:
>>   subroutine ForSR(astr, bstr)
>>   character astr*1, bstr*2
>>   ....

>>Then, to call this routine from a C program, should one use:
>>forsr_(char *cpA, int *iA, char* cpB, int* iB)

>>where cpA="Q", cpB="XY", iA=1, iB=2?

> As I explained this at some length the other day in sci.math.num-analysis,
> this is OT in comp.lang.fortran. What part of that did you not understand ?

    I disagree, fairly strongly.  Folks in comp.lang.fortran are often
confronted with mixed-language programming and are generally willing
to help out.

    To the question H.S. posed, it is highly Fortran compiler dependent
as to how the "hidden" length arguments are passed.  Some compilers
do as you've described, putting the length arg directly following the
character (address of the character buffer) arg.  Others put all length
args in order at the end of the formal argument list.  Still others
pass a structure, "dope vector" if you will, as a single argument that
includes the length and the address.

    The argument passing mechanism should be in your compiler's
documentation, but if you're using one of the "popular" compilers,
denizens of c.l.f may know off the top of their heads. :-)  Therefore,
please post the platform (HW & O/S) and compiler (brand and version)
that you're using [unless, of course, you already did so and I'm way
behind the game, sigh...].

     -Ken
--
I don't speak for Intel, Intel doesn't speak for me...

Ken Fairfield
D1C Automation VMS System Support
who:   kenneth dot h dot fairfield
where: intel dot com



Mon, 24 Sep 2007 02:09:47 GMT  
 Fortran subroutine characters args and their lengths and C
Apparently, _Ken Fairfield_, on 04/06/2005 02:09 PM,typed:

Quote:
>    To the question H.S. posed, it is highly Fortran compiler dependent
> as to how the "hidden" length arguments are passed.  Some compilers
> do as you've described, putting the length arg directly following the
> character (address of the character buffer) arg.  Others put all length
> args in order at the end of the formal argument list.  Still others
> pass a structure, "dope vector" if you will, as a single argument that
> includes the length and the address.

>    The argument passing mechanism should be in your compiler's
> documentation, but if you're using one of the "popular" compilers,
> denizens of c.l.f may know off the top of their heads. :-)  Therefore,
> please post the platform (HW & O/S) and compiler (brand and version)
> that you're using [unless, of course, you already did so and I'm way
> behind the game, sigh...].

>     -Ken

I am using g77 on a Linux 2.4.25 kernel. g77 is ver 3.2. Linux is
running on Intel Pentium III (Katmai) (or on Intel Pentium 4, depending
where I am logged on on a given day in my university). While compiling
the C program, which calls the Fortran code, I am using the g2c library.

Others have also mentioned how the arg calling is implementation
dependent. I am currently reading the g77 documentation to find out if
it resolves my doubts.

Thanks for the helpful reply,
->HS
--
(Remove all underscores,if any, from my email address to get the correct
one. Apologies for the inconvenience but this is to reduce spam.)



Mon, 24 Sep 2007 02:49:25 GMT  
 Fortran subroutine characters args and their lengths and C

Quote:

>Others have also mentioned how the arg calling is implementation
>dependent. I am currently reading the g77 documentation to find out if
>it resolves my doubts.

g77, like almost all Unix compilers, puts the hidden args on the end.

-- greg



Mon, 24 Sep 2007 02:49:01 GMT  
 
 [ 34 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. Question about # of SUBROUTINE args in OpenVMS FORTRAN

2. Fortran defered length character string?

3. Vax Fortran calling C subroutine, need to return a character string

4. Passing logical variables from Fortran subroutine to C subroutine

5. length of subroutine names

6. Passing character arrays as subprogram args

7. Fortran subroutine to run in ADAMS/Solver FORTRAN

8. Spanish characters have no length?

9. variable-length character array.

10. Find the length of a character field

11. maximum supported length of characters in a table

12. fixed length character array passed as a real argument

 

 
Powered by phpBB® Forum Software