replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Character variable declaration
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Character variable declaration

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Fri Jun 22, 2012 9:50 am    Post subject: Character variable declaration Reply with quote

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

1)
Code:
character*(*) arg


or in form

2)
Code:
character*256 arg


Does 1 indicate an array of characters arg?
Back to top
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: D�sseldorf, Germany

PostPosted: Fri Jun 22, 2012 5:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2402
Location: Yateley, Hants, UK

PostPosted: Fri Jun 22, 2012 8:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group