View previous topic :: View next topic |
Author |
Message |
simon
Joined: 05 Jul 2006 Posts: 268
|
Posted: Fri Mar 11, 2022 7:51 am Post subject: Trim in a subroutine argument |
|
|
The following module defines a subroutine with a character array as an argument, and a simple program calls the subroutine. The call passes a scalar character, but uses square brackets to treat the scalar as an array with just one element. The first call compiles ok, but the second call uses the intrinsic function Trim, generating a compilation error message. I understand that Trim could not be used if there were multiple elements to c of different length, but since there is only one the error message that "Array constructor elements must be of fixed length" seems misleading if not erroneous. I don't know what the standard says, but since "Trim(c)" returns a character scalar, and "c" without Trim is a character scalar, it seems reasonable to assume that the second call should compile. The code compiles and runs with the intended result with NAGWare.
Code: | Module m
Contains
Subroutine s (c)
Character(Len=*), Dimension(:), Intent(In) :: c
End Subroutine s
End Module m
Program p
Use m
Character(Len=4) :: c = 'c '
Call s ([c])
Call s ([Trim(c)])
End Program p
|
|
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7924 Location: Salford, UK
|
Posted: Fri Mar 11, 2022 10:42 am Post subject: |
|
|
Simon
This code fails to compile with FTN95 but is OK with other compilers that I have tried.
It compiles if you use /ignore 1076 but gives incorrect results for 64 bits and a runtime failure for 32 bits.
As far as the Standard is concerned, it is possible that the FTN95 error report is correct for Fortran 95 but was relaxed in a later standard.
It seems strange to me that one would use TRIM in an array constructor or that one would want to pass an array constructor as an argument.
Perhaps as a work-around you could apply the TRIM within the subroutine. |
|
Back to top |
|
|
simon
Joined: 05 Jul 2006 Posts: 268
|
Posted: Fri Mar 11, 2022 6:31 pm Post subject: |
|
|
Thanks for the response, Paul.
I can see that the use of Trim in the array constructor is somewhat problematic. I'll look into a work around. |
|
Back to top |
|
|
|