Character variable variable passed as a subroutine argument 
Author Message
 Character variable variable passed as a subroutine argument

Showing my ignorance no doubt, but I have just discovered that in the
following situation (ie dummy argument longer than actual argument):

      character(9) A
      call sub(A)
      A = 'catcatcat'
      ....

      subroutine x()
      character(20) B
      ......
      entry sub(B)

then B has a value 'catcatcat'//NUL//SOH// with the rest NUL
characters

ie. write(10,'(a)')  B prints out "catcatcat^#^^^^^^^^^"
where ^ = Z'20'  and # = Z'01'

& worse (my downfall) TRIM(B) = "catcatcat^#"

Is this expected behaviour (CVF 6.6A)?    (or does any thing go?)

(silly Qn I guess - so fundamental CVF has to have it correct?)

Declaring as character(*) B

fixes the problem - but here I have been thinking it best not to use
assumed-length character arguments.  Not certain why, other than that
the CVF manual states that "The form CHARACTER*(*) is an obsolescent
feature in fortran 95" ?

?? Does that last statement mean that I should be avoiding
assumed-length character arguments [ah la character(*) B ], or is it
referring to some other usage?

Following the advice has been a rather negative experience in this
case.

David



Tue, 02 May 2006 12:16:54 GMT  
 Character variable variable passed as a subroutine argument
Oops!  I really messed that up

1.  make those first 3 lines of code:

      character(9) A
      A = 'catcatcat'
      call sub(A)

2.  and where ^ = Z'20' , should of course read

where ^ = Z'00'  !!!!

One pain in the b**t I forgot to mention, is that the Z'00' &  Z'01'
characters do not show in the de{*filter*} watch window - ie. the watch
window shows 11 spaces (Z'20') following 'catcatcat'.  Did not make it
easy to debug the problem.

David



Quote:
>Showing my ignorance no doubt, but I have just discovered that in the
>following situation (ie dummy argument longer than actual argument):

>      character(9) A
>      call sub(A)
>      A = 'catcatcat'
>      ....

>      subroutine x()
>      character(20) B
>      ......
>      entry sub(B)

>then B has a value 'catcatcat'//NUL//SOH// with the rest NUL
>characters

>ie. write(10,'(a)')  B prints out "catcatcat^#^^^^^^^^^"
>where ^ = Z'20'  and # = Z'01'

>& worse (my downfall) TRIM(B) = "catcatcat^#"

>Is this expected behaviour (CVF 6.6A)?    (or does any thing go?)

>(silly Qn I guess - so fundamental CVF has to have it correct?)

>Declaring as character(*) B

>fixes the problem - but here I have been thinking it best not to use
>assumed-length character arguments.  Not certain why, other than that
>the CVF manual states that "The form CHARACTER*(*) is an obsolescent
>feature in Fortran 95" ?

>?? Does that last statement mean that I should be avoiding
>assumed-length character arguments [ah la character(*) B ], or is it
>referring to some other usage?

>Following the advice has been a rather negative experience in this
>case.

>David



Tue, 02 May 2006 12:51:48 GMT  
 Character variable variable passed as a subroutine argument

Quote:
> Declaring as character(*) B
> fixes the problem - but here I have been thinking it best not to use
> assumed-length character arguments.  Not certain why, other than that
> the CVF manual states that "The form CHARACTER*(*) is an obsolescent
> feature in Fortran 95" ?
> ?? Does that last statement mean that I should be avoiding
> assumed-length character arguments [ah la character(*) B ], or is it
> referring to some other usage?

It's
      CHARACTER*(*) B
that is obsolescent, not
      CHARCTER(*) B
so use the latter, modern form, rather than the former,
obsolescent form.  Both mean the same thing.  Try both
with /stand:f95 enabled and check the warning status.
Check results of both forms.

--
write(*,*) transfer((/17.392111325966148d0,3.6351694777236872d228, &
6.0134700169991705d-154/),(/'x'/)); end



Tue, 02 May 2006 13:01:20 GMT  
 Character variable variable passed as a subroutine argument
Only the actual syntactic form of CHARACTER*(*) is obsolesecent -
which means "going to be obsolete", i.e., it's not obsolete yet.

If you had an explicit interface for your subroutine in your program
- for instance, by putting them into a module - the compiler would
have told you what was going wrong. Using the assumed-length form for
the dummy argument is just the right thing to do here.

        Jan



Tue, 02 May 2006 18:24:02 GMT  
 Character variable variable passed as a subroutine argument

Quote:

> Declaring as character(*) B

> fixes the problem - but here I have been thinking it best not to use
> assumed-length character arguments.  Not certain why, other than that
> the CVF manual states that "The form CHARACTER*(*) is an obsolescent
> feature in Fortran 95" ?

> ?? Does that last statement mean that I should be avoiding
> assumed-length character arguments [ah la character(*) B ], or is it
> referring to some other usage?

Assumed length character dummys are the only reasonable (IMO) way to
go in my opinion.  I look askance at any dummy character argument
that isn't assumed length.

As others have pointed out, there is nothing obsolescent about assumed
length characters - just one particular syntax of declaring them.
(And personally, I use the obsolescent syntax anyway, just because I'm
used to it.  That's the only obsolescent thing that I allow myself).

--
Richard Maine
email: my last name at domain
domain: isomedia dot com



Wed, 03 May 2006 04:50:54 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing logical variables from Fortran subroutine to C subroutine

2. passing functions with variable number of arguments as procedure arguments

3. Subroutine arguments vs module variables

4. variable # arguments in subroutine call

5. one subroutine for variable arguments

6. Variable argument list in subroutines

7. pass a subroutine (use derived data type) as an argument to another subroutine

8. Q. Passing variable dimensioned arrays to subroutines.

9. Programming Style - Passing Variables to Subroutines

10. passing awk variables as arguments

11. Pass variable arguments?

12. passing by reference to variable argument functions

 

 
Powered by phpBB® Forum Software