Passing a two dimentional array from Fortran to C

前端 未结 2 1428
無奈伤痛
無奈伤痛 2020-12-11 13:17

I am having trouble passing a two dimensional array from Fortran to C. The following is my C function which just displays the array elements on the screen.

#         


        
2条回答
  •  离开以前
    2020-12-11 13:38

    There are a few issues in the code as [currently] shown.

    • The n argument in the Fortran interface for print2 does not have the VALUE attribute, yet the corresponding parameter in the C function is taken by value. Consider adding VALUE to the Fortran declaration.

    • The same issue arises with the pointer to the array. The Fortran interface passes a pointer without value, the C function expects a "pointer by value" (as opposed to a pointer to a pointer). Note that there is no need to explicitly use a C_PTR here - you can construct an interoperable interface using the actual type of the array.

    • On most platforms a Fortran default REAL is not the same as a C double - consider using the kind constants from ISO_C_BINDING to ensure that the kind of the REAL on the Fortran side matches that of the C.

    • C_LOC requires its argument to have the TARGET attribute. Add that attribute to the declaration of the a variable in the main program.

提交回复
热议问题