home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / fortran / 4383 < prev    next >
Encoding:
Internet Message Format  |  1992-11-20  |  1.7 KB

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!kuhub.cc.ukans.edu!husc-news.harvard.edu!husc8!dvalent
  2. Newsgroups: comp.lang.fortran
  3. Subject: Re: passing a string constant to a subroutine
  4. Message-ID: <dvalent.722283516@husc8>
  5. From: dvalent@husc8.harvard.edu (Daniel Valentine)
  6. Date: 20 Nov 92 18:18:36 GMT
  7. References: <EJH.92Nov19160031@khonshu.colorado.edu>
  8. Nntp-Posting-Host: husc8.harvard.edu
  9. Lines: 33
  10.  
  11. ejh@khonshu.colorado.edu (Edward J. Hartnett) writes:
  12.  
  13. >When I want to pass a string constant to a subroutine, what size do I
  14. >declare the character variable to be in the subprogram. For example
  15. >I have a subroutine:
  16.  
  17. >      subroutine ask_data_type(prog, typenum)
  18.  
  19. >This is intended to be called from one of several programs, and the
  20. >program name is to be passed in as the first parameter. Inside the
  21. >subroutine I have:
  22. >      character*20 prog
  23.  
  24. >And when I called if from program atob.f this is what the call looked
  25. >like:
  26. >    call ask_data_type('atob',typenum)
  27.  
  28. >I assumed that it would put 'atob' in prog and blank fill the rest.
  29. >What it did instead was fill the rest with grabage, not blanks.
  30.  
  31. Because you declared the string as character*20 in the subroutine, the
  32. subroutine took the address of the string from the descriptor you passed
  33. and assumed that the 20 bytes starting there were your string. (FORTRAN
  34. tends to pass addresses and descriptors rather than creating new copies
  35. of data on the stack.)
  36.  
  37. If you want to be able to pass variable length strings to your subroutine,
  38. make a declaration like:
  39.         character*(*) prog,
  40. which will take the length from the descriptor as well.
  41.  
  42.                                -- Dan Valentine
  43.                                   dvalent@husc.harvard.edu
  44.