Lahey/GNU Fortran

Next: , Previous: Intrinsic Types, Up: Interoperability with C


7.1.2 Derived Types and struct

For compatibility of derived types with struct, one needs to use the BIND(C) attribute in the type declaration. For instance, the following type declaration

      USE ISO_C_BINDING
      TYPE, BIND(C) :: myType
        INTEGER(C_INT) :: i1, i2
        INTEGER(C_SIGNED_CHAR) :: i3
        REAL(C_DOUBLE) :: d1
        COMPLEX(C_FLOAT_COMPLEX) :: c1
        CHARACTER(KIND=C_CHAR) :: str(5)
      END TYPE

matches the following struct declaration in C

      struct {
        int i1, i2;
        /* Note: "char" might be signed or unsigned.  */
        signed char i3;
        double d1;
        float _Complex c1;
        char str[5];
      } myType;

Derived types with the C binding attribute shall not have the sequence attribute, type parameters, the extends attribute, nor type-bound procedures. Every component must be of interoperable type and kind and may not have the pointer or allocatable attribute. The names of the variables are irrelevant for interoperability.

As there exist no direct Fortran equivalents, neither unions nor structs with bit field or variable-length array members are interoperable.