You can pass individual members of a FORTRAN or BASIC common block in an argument list, just as you can any data item. However, you can also give a different language module access to the entire common block at once.
C or C++ modules can reference the items of a common block by first declaring a structure with fields that correspond to the common-block variables. Having defined a structure with the appropriate fields, the C or C++ module must then connect with the common block itself. The next two sections present methods for gaining access to common blocks.
Passing the Address of a Common Block
To pass the address of a common block, simply pass the address of the first variable in the block. (In other words, pass the first variable by reference.) The receiving C or C++ module should expect to receive a structure by reference.
In the example below, the C function initcb receives the address of the variable N, which it considers to be a pointer to a structure with three fields:
C FORTRAN SOURCE CODE
C
COMMON /CBLOCK/N, X, Y
INTEGER*2 N
REAL*8 X, Y
.
.
.
CALL INITCB( N )
/* C source code */
/* Explicitly set structure packing to word-alignment */
#pragma pack( 2 )
struct block_type
{
int n;
double x;
double y;
};
initcb( struct block_type * block_hed )
{
block_hed->n = 1;
block_hed->x = 10.0;
block_hed->y = 20.0;
}
Accessing Common Blocks Directly
You can access FORTRAN common blocks directly by defining a structure with the appropriate fields and then using the methods described in “External Data”. Here is an example of accessing common blocks directly:
struct block_type
{
int n;
double x;
double y;
};
extern struct block_type fortran cblock;
Summary: You cannot access common blocks directly using BASIC common blocks.
Note that the technique of accessing common blocks directly works with FORTRAN common blocks, but not with BASIC common blocks. If your C or C++ module must work with both FORTRAN and BASIC common blocks, pass the address of the common block as a parameter to the function..