Re: Transmission of character string by argument
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: Transmission of character string by argument         

Group: comp.lang.fortran · Group Profile
Author: e 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
no comments
diggit! del.icio.us! reddit!