External Interfaces/API Reference    
Passing Pointers in Fortran


The MATLAB API works with a unique data type, the mxArray. Because there is no way to create a new data type in Fortran, 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, i.e., 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:

  1. The %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.

  1. Variable declarations

    To use pointers properly, you must declare them to be the correct size. On DEC Alpha 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.


 The MATLAB Array  DDE Functions