Silverfrost Forums

Welcome to our forums

Character variable declaration

22 Jun 2012 8:50 #10391

Please can anyone tell me what is the difference between declaring a character variable in form:

1) character*(*) arg

or in form

2) character*256 arg

Does 1 indicate an array of characters arg?

22 Jun 2012 4:12 #10395

Hi,

there are several ways to declare character variables. A short overview for instance is given here: http://www.obliquity.com/computer/fortran/datatype.html (scroll down to see more about character*(*) declaration).

A simple string:

character*120  string_1
string_1 = 'abcd.........................'  ! length = 120 characters

A character array:

character*1 string_2(120)
string_2(1) = 'a'
string_2(2) = 'b'
!...
string_2(120) = 'x'

Another character array:

character*4 string_3(25)
string_3(1) = 'abcd'
string_3(2) = 'efgh'
!...

Regards - Wilfried

22 Jun 2012 7:26 #10400

If you are using character strings, you have to declare their lengths somewhere. You can use CHARACTER64 or CHARACTER(64) - these mean the same thing, and the form with brackets around is just to make it more consistent with other things in Fortran 90 and onwards.

CHARACTER*(*) is found in a subprogram that has to accept strings of different lengths. Consider the Silverfrost routine UPCASE@, which converts a string to upper case (capitals). This works on any length string. So, if you had:

      PROGRAM CL
      CHARACTER*(7) Forename
      CHARACTER*(6) Surname
      Forename = 'Christy'
      Surname = 'Leomin'
      CALL UPCASE@ (Forename)
      CALL UPCASE@ (Surname)
      WRITE(*,'(A7,1X,A6)') Forename, Surname
      END

You could work out what you would get! The UPCASE@ routine is something you could easily write yourself (although you would not be able to use @). Inside it, you wouldn't know how big to specifiy the parameter that is Forename at first and Surname later. So you would begin with:

      SUBROUTINE UPCASE (Name)
      CHARACTER*(*) Name
      INTEGER LENGTH
      LENGTH = LEN(Name) 
      and so on

If you specified the exact length inside the subroutine, it would only work up to that many characters. Using CHARACTER*(*) makes it work with any number of characters you throw at it.

There is also a form of the declaration like this:

  CHARACTER (LEN=20) :: Name

which is again more in line with Fortran 95.

E

Please login to reply.