Fortran Programming Guide

Structures

C and FORTRAN 77 structures and Fortran 90 derived types can be passed to each other's routines as long as the corresponding elements are compatible.

Table 11-9 Passing FORTRAN 77 STRUCTURE Records

Fortran calls C 

C calls Fortran 

STRUCTURE /POINT/

REAL X, Y, Z

END STRUCTURE

RECORD /POINT/ BASE

EXTERNAL FLIP

...

CALL FLIP( BASE )

...

------------------------------

struct point {

float x,y,z;

};

void flip_( struct point *v; )

{

float t;

t = v -> x;

v -> x = v -> y;

v -> y = t;

v -> z = -2.*(v -> z);

}

struct point {

float x,y,z;

};

void fflip_ ( struct point *) ;

...

struct point d;

struct point *ptx = &d;

...

fflip_ (ptx);

...

------------------------------

SUBROUTINE FFLIP(P)

STRUCTURE /POINT/

REAL X,Y,Z

END STRUCTURE

RECORD /POINT/ P

REAL T

T = P.X

P.X = P.Y

P.Y = T

P.Z = -2.*P.Z

...

Table 11-10 Passing Fortran 90 Derived Types

Fortran 90 calls C 

C calls Fortran 90 

TYPE point

SEQUENCE

REAL :: x, y, z

END TYPE point

TYPE (point) base

EXTERNAL flip

...

CALL flip( base)

...

------------------------------

struct point {

float x,y,z;

};

void flip_( struct point *v; )

{

float t;

t = v -> x;

v -> x = v -> y;

v -> y = t;

v -> z = -2.*(v -> z);

}

struct point {

float x,y,z;

};

extern void fflip_ (

struct point *) ;

...

struct point d;

struct point *ptx = &d;

...

fflip_ (ptx);

...

------------------------------

SUBROUTINE FFLIP( P )

TYPE POINT

REAL :: X, Y, Z

END TYPE POINT

TYPE (POINT) P

REAL :: T

T = P%X

P%X = P%Y

P%Y = T

P%Z = -2.*P%Z

...