For Programmers: Free Programming Magazines  


Home > Archive > Fortran > June 2004 > Passing C structures as arguments









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Passing C structures as arguments
Mike Walters

2004-06-03, 7:24 pm

Hi,

I have a C function which has as its arguments pointers to some C
structures. The structures are defined in a C header file. I am
wondering if it is possible to pass in these arguments from a Fortran
program. I know you can "share" structures through modules and/or
common blocks, but that is not what I want. I want to pass them as
arguments because I don't want to rewrite the C function. Is this
possible, or do have to wait for Fortran 2003?

I am using the Intel Fortran Compiler 8.0 on Red Hat Linux 7.2.

Thanks

--

I'm Mike Walters, and I approved this message.

Richard Maine

2004-06-03, 7:24 pm

Mike Walters <walters@wisp.physics.YOURwisc.eduPANTS> writes:

> I have a C function which has as its arguments pointers to some C
> structures. The structures are defined in a C header file. I am
> wondering if it is possible to pass in these arguments from a Fortran
> program. I know you can "share" structures through modules and/or
> common blocks, but that is not what I want. I want to pass them as
> arguments because I don't want to rewrite the C function. Is this
> possible, or do have to wait for Fortran 2003?
>
> I am using the Intel Fortran Compiler 8.0 on Red Hat Linux 7.2.


It is probably possible - just not portable. There are 2 unrelated
issues here.

1. The structure. You'll need to make a Fortran derived type that
appropriately matches the memory layout of the C struct. You
probably should make it a sequence type (which might or might not
make any real difference). Padding might or might not be an
issue, depending on the particular compilers. Of course, if
your structure contains things that don't have good Fortran
analogs, then life is much moe complicated. (C pointers are
prime candidates for problems here).

I can't give a cookbook for every possible type, and I don't even
know the exact details for the specific compiler in question, but
it has reasonable odds of being workable.

Basically, the f2003 stuff allows you to do this portably, but
odds are at least reasonable that you can already do it in f95,
as long as you do your own type matching (and if the structure
doesn't run into alignment/padding inconsistencied.)

2. Your subject line didn't mention the word "pointer", but the body
did. Be aware that Fortran pointers are *NOT* the same thing as
C pointers. There's at least half a chance that the two might
be compatible for scalars, but almost no chance for arrays.
You may need to play compiler-specific games for the array case,
manually passing the address of the array by value. Or perhaps
making a scalar pointer to the first element of the array might
work (assuming the array is contiguous - if it isn't then C just
isn't going to be able to deal with it).

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com