views:

352

answers:

1

Using Visual Studio 9 on Windows 64 with Intel Fortran 10.1

I have a C function calling Fortran, passing a literal string "xxxxxx" (not null terminated) and the hidden passed length arg 6.

Fortran gets it right since the debugger recognizes it's a character(6) var and has the correct string, but when I try to assign another Fortran character*6 var to it I get the oddest error.

forrtl: severe (408): fort: (4): Variable Vstring has substring ending point 6 which is greater than the variable length 6

-- C call --

SETPR("abcdef",6);

-- Fortran subroutine --

subroutine setpr(vstring)

character*(*) vstring

character*6 prd

prd(1:6) = vstring(1:6)

return

end
+1  A: 

I tried this with the Intel C compiler and the Intel Fortran compiler. This gave, in C,

#include <stdio.h>

int main(void)
{
    extern void test_f_(char*, int);

    test_f_("abcdef",6);
}

and, in Fortran,

subroutine test_f(s)
    implicit none
    character*(*), intent(in) :: s

    character*6 :: c

    write (*,*) 'S is ', s
    write (*,*) 'Length of S is', len(s)

    c = s
    write (*,*) 'Implicit-copied C is ', c

    c(1:6) = s(1:6)
    write (*,*) 'Range-copied C is ', c
end subroutine test_f

When compiled and run, it produces

S is abcdef
Length of S is           6
Implicit-copied C is abcdef
Range-copied C is abcdef

What is your declaration in the C routine for the type of the Fortran routine? Are you sure that the sizes of character and integer variables are the same between the C and Fortran code?

Tim Whitcomb
I'm not using the Intel C compiler here. That works fine on the Linux version. This problem is with the MSVC compiler on Windows.
Is the declaration in C of the Fortran routine the same as shown here?
Tim Whitcomb
Yes and no. The original is code I cannot share because the company is paranoid about releasing source. This is just one way to see it fail in the same manner.