Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29602 Обсуждение

How to deal with variable-sized array in fortran

congcongkx
Начинающий
1 647Просмотр.
I want to call a subroutine in fortran from C++. but this subroutine has an array with variable length.
Here is a example:

SUBROUTINE foo(A)
INTEGER, DIMENSION(:), INTENT(IN) :: A
INTEGER :: len
...
len = size(A)
...
END SUBROUTINE foo

as we can see, the size of A can be obtained through size(). but this works only with array created in fortran. If such routine is called from C/C++, how could I pass the information of array size?
thanks a lot.
0 баллов
1 Ответить
Steven_L_Intel1
Сотрудник
1 647Просмотр.
If you use a deferred-shape array, with (:) bounds, you will need to have the C code pass a Fortran array descriptor. The details of this are in the mixed-language programming section of the User Manual (or Programmer's Guide for CVF.)

However, a much simpler approach is to write the Fortran routine as follows:

SUBROUTINE foo(A,N)
INTEGER, INTENT(IN) :: N
INTEGER, DIMENSION(N), INTENT(IN) :: A
INTEGER :: len

and pass the upper bound of the array from the C code as an ordinary integer variable (passed by reference here). Do keep in mind that C arrays start at index 0, while Fortran starts at 1, so you may need to make adjustments. Or you could declare the Fortran array (0:N).
Ответить