home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!kuhub.cc.ukans.edu!husc-news.harvard.edu!husc8!dvalent
- Newsgroups: comp.lang.fortran
- Subject: Re: passing a string constant to a subroutine
- Message-ID: <dvalent.722283516@husc8>
- From: dvalent@husc8.harvard.edu (Daniel Valentine)
- Date: 20 Nov 92 18:18:36 GMT
- References: <EJH.92Nov19160031@khonshu.colorado.edu>
- Nntp-Posting-Host: husc8.harvard.edu
- Lines: 33
-
- ejh@khonshu.colorado.edu (Edward J. Hartnett) writes:
-
- >When I want to pass a string constant to a subroutine, what size do I
- >declare the character variable to be in the subprogram. For example
- >I have a subroutine:
-
- > subroutine ask_data_type(prog, typenum)
-
- >This is intended to be called from one of several programs, and the
- >program name is to be passed in as the first parameter. Inside the
- >subroutine I have:
- > character*20 prog
-
- >And when I called if from program atob.f this is what the call looked
- >like:
- > call ask_data_type('atob',typenum)
-
- >I assumed that it would put 'atob' in prog and blank fill the rest.
- >What it did instead was fill the rest with grabage, not blanks.
-
- Because you declared the string as character*20 in the subroutine, the
- subroutine took the address of the string from the descriptor you passed
- and assumed that the 20 bytes starting there were your string. (FORTRAN
- tends to pass addresses and descriptors rather than creating new copies
- of data on the stack.)
-
- If you want to be able to pass variable length strings to your subroutine,
- make a declaration like:
- character*(*) prog,
- which will take the length from the descriptor as well.
-
- -- Dan Valentine
- dvalent@husc.harvard.edu
-