0

I have a Fortran subroutine that expects a complex array like

subroutine foo(cnumbers, n)
    integer :: n
    complex :: cnumbers(n)
    ...
end subroutine foo

and later I want to call it like

real :: rnumbers(40)
...
call foo(rnumbers, 20)

However, I get the compiler error:

error #6633: The type of the actual argument differs from the type of the dummy argument.

Of course, this is comprehensible since a real array is not a complex array. But there must be a way to make it work.

Because if the subroutine foo and the call of foo are in different modules and are written down in different Fortran files, then the compiler does not complain, and everything works fine.

Does someone know how to make it work? How to pass a real array if a complex array is expected?

3
  • You should show more code on the calling side. If rnumbers is not a dummy argument you can just equivalence it with a complex array. Commented May 2, 2016 at 8:28
  • No, rnumbers is not dummy, its an actual variable that I create with real :: rnumbers(40). By the way, I am using the Intel Fortran compiler 15.0.3. Commented May 2, 2016 at 8:37
  • 1
    if your subroutine was external (no explicit interface) your compiler would not detect the type issue and it would work fine. Commented May 2, 2016 at 15:37

1 Answer 1

2

You can either use transfer(rnumbers, ...) to convert the type (a temporary array is likely to be created) or use equivalence to avoid it

    real :: rnumbers(40)
    complex :: cnumbers(20)
    equivalence (rnumbers, cnumbers)

    set the value of rnumbers

    call foo(cnumbers, 20)

If you need allocatable arrays the equivalence will not work.

You can also use an external subroutine and lie the compiler about the interface and just pass the real array instead of the complex one. It is not standard conforming, but it is sometimes used. See also Gfortran complex actual to real dummy argument

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the answer. But unfortunately I deal with allocatable arrays. Is there no workaround with pointers or so? I gues the solution with transfer is to expensive when it comes to very large arrays.
@thyme That's exactly why I requested that you add more of the calling code! Why you simply can't do that? I sort of assumed that the code you posted is incomplete and added the disclaimer... The only thing you did is that you just replied "rnumbers is not dummy, its an actual variable that I create with real :: rnumbers(40)" even if it is not true at all...
Ok, sorry, I was too naive. I thought to make the example as simple as possible...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.