| MATLAB Application Program Interface Reference | Help Desk |
Passing Pointers in Fortran
The MATLAB API works with a unique data type, themxArray. Because there is no way to create a new data type in Fortran 77, MATLAB passes a special identifier, called a pointer, to a Fortran program. You can get information about an mxArray by passing this pointer to various API functions (called "Access Routines"). These access routines allow you to get a native Fortran data type containing exactly the information you want -- the size of the mxArray, whether or not it is a string, or its data contents.
There are several implications when using pointers in Fortran:
%VAL construct.
If your Fortran compiler supports the %VAL construct, then there is one type
of pointer you can use without requiring an access routine, namely a pointer
to data (i.e., the pointer returned by mxGetPr or mxGetPi). You can use %VAL
to pass this pointer's contents to a subroutine, where it is declared as a Fortran double-precision array.
If your Fortran compiler does not support the %VAL construct, you must use
the mxCopy__ routines (e.g., mxCopyPtrToReal8) to access the contents of the
pointer.
To use pointers properly, you must declare them to be the correct size. On
DEC Alpha and 64-bit SGI machines, all pointers should be declared as
integer*8. On all other platforms, pointers should be declared as
integer*4.
If your Fortran compiler supports preprocessing with the C preprocessor,
you can use the preprocessing stage to map pointers to the appropriate declaration. In UNIX, see the examples ending with .F in the examples directory for a possible approach.
Note: Declaring a pointer to be the incorrect size can cause your program to crash.