View previous topic :: View next topic |
Author |
Message |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Fri Jun 22, 2012 9:50 am Post subject: Character variable declaration |
|
|
Please can anyone tell me what is the difference between declaring a character variable in form:
1)
or in form
2)
Does 1 indicate an array of characters arg? |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Fri Jun 22, 2012 5:12 pm Post subject: |
|
|
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:
Code: | character*120 string_1
string_1 = 'abcd.........................' ! length = 120 characters |
A character array:
Code: | character*1 string_2(120)
string_2(1) = 'a'
string_2(2) = 'b'
!...
string_2(120) = 'x' |
Another character array:
Code: | character*4 string_3(25)
string_3(1) = 'abcd'
string_3(2) = 'efgh'
!... |
Regards - Wilfried |
|
Back to top |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2402 Location: Yateley, Hants, UK
|
Posted: Fri Jun 22, 2012 8:26 pm Post subject: |
|
|
If you are using character strings, you have to declare their lengths somewhere. You can use CHARACTER*64 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:
Code: | 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:
Code: | 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 |
|
Back to top |
|
 |
|