| Re: Transmission of character string by argument |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.fortran · Group Profile
Author: e p chandlere p chandler Date: Apr 15, 2008 05:19
On Apr 15, 3:10Â am, bru wrote:
> Hello,
>
> Â Â Â Â I do not understand why I have this fatal error when compiling the
> following test code with NAG f95
>
> Â Â Â Â PROGRAM TST_STRING
>
> Â Â Â Â CALL SUBSTR('HELLO THE WETHER IS NICE')
> Â Â Â Â CALL SUBSTR('HELLO THE WETHER IS ')
> Â Â Â Â CALL SUBSTR('HELLO THE WETHER ')
> Â Â Â Â STOP
>
> Â Â Â Â END
> Â Â Â Â SUBROUTINE SUBSTR(STRNG)
> Â Â Â Â CHARACTER(24) STRNG
>
> Â Â Â Â WRITE(6,*) 'STRING=', STRNG
> Â Â Â Â RETURN
>
> Â Â Â Â END
>
> Error: tst_string.f: Argument STRNG (no. 1) in reference to SUBSTR from
> TST_STRING is too short a character string
> [f95 error termination]
> ccali05{bru}54:
>
> Â Â Â Â The number of characters is less or equal 24 !!!!!
> Â Â Â Â Nevertheless it runs ok with Sun f90, f77; ifort; pgf95 pgf77 ; g95,g77
> (for fortran 77 I put CHARACTER*24)
>
> Â Â Â Â The above code works with NAG f95 if in the subroutine I put
> CHARACTER(*) STRNG
>
> Â Â Â Â Regards
>
> B Bru
1. Compilers and run time libraries are not required to detect this
type of argument mismatch.
With g95 I see
C:\Users\epc\temp>g95 foo.f
C:\Users\epc\temp>a
STRING=HELLO THE WETHER IS NICE
STRING=HELLO THE WETHER IS HELL
STRING=HELLO THE WETHER foo.f S
C:\Users\epc\temp>
Note that the subroutine is displaying characters that are OUT OF
BOUNDS.
2. Making the subroutine an internal subprogram is ONE WAY to force
arguments to match.
C:\Users\epc\temp>type foo1.f
PROGRAM TST_STRING
CALL SUBSTR('HELLO THE WETHER IS NICE')
CALL SUBSTR('HELLO THE WETHER IS ')
CALL SUBSTR('HELLO THE WETHER ')
STOP
CONTAINS
SUBROUTINE SUBSTR(STRNG)
CHARACTER(24) STRNG
WRITE(6,*) 'STRING=', STRNG
RETURN
END SUBROUTINE
END PROGRAM
C:\Users\epc\temp>g95 foo1.f
In file foo1.f:4
CALL SUBSTR('HELLO THE WETHER IS ')
1
Warning (135): Actual character argument at (1) is shorter in length
than the fo
rmal argument
In file foo1.f:5
CALL SUBSTR('HELLO THE WETHER ')
1
Warning (135): Actual character argument at (1) is shorter in length
than the fo
rmal argument
C:\Users\epc\temp>
3. A CHARACTER(*) argument is NOT a variable length string. It is a
fixed length string whose length is fixed by the calling routine.
- e
|