home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / fortran / 4390 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.9 KB  |  73 lines

  1. Newsgroups: comp.lang.fortran
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!nntp-server.caltech.edu!draco.macsch.com!convex.is.macsch.com!dnl
  3. From: dnl@convex.is.macsch.com (David Lombard)
  4. Subject: Re: passing a string constant to a subroutine
  5. Message-ID: <1992Nov20.211938.4012@draco.macsch.com>
  6. Sender: usenet@draco.macsch.com (Usenet Poster)
  7. Organization: MacNeal-Schwendler Corp.
  8. References: <EJH.92Nov19160031@khonshu.colorado.edu>
  9. Date: Fri, 20 Nov 92 21:19:38 GMT
  10. Lines: 61
  11.  
  12. In article <EJH.92Nov19160031@khonshu.colorado.edu> ejh@khonshu.colorado.edu (Edward J. Hartnett) writes:
  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. I
  30. >avoid this problem now by doing this in the calling program:
  31. >
  32. >      character*20 prog
  33. >      parameter (prog = 'atob')
  34. >c
  35. >      write(*,*) 'about to call ask_data_type'
  36. >      call ask_data_type(prog,typenum)
  37. >
  38. >But I'm just wondering if what I did was non-standard or what.
  39. >
  40. Perfectly standard, just incorrect!  Your declaration:
  41.     character*20 prog
  42.  
  43. can (and was in your example) the problem.  As a rule, *always*
  44. declare all character dummy arguments as:
  45.     character*(*) prog
  46.  
  47. As for why the *garbage*, read 15.9.3.1 (pg 15-17) from the standard:
  48.     If a dummy argument is of type character, the associated 
  49.     actual argument must be of type character and the length of
  50.     the dummy argument must be less than or equal to the length
  51.     of the actual argument.  If the length _len_ of a dummy
  52.     argument of type character is less than the length of an
  53.     associated actual argument, the leftmost _len_ characters of
  54.     the actual argument are associated with the dummy argument.
  55.  
  56. Translation: the subroutine must use a smaller length than the
  57. calling routine.  If the subroutine's length is less, the subroutine
  58. cannot see the characters beyond the subroutine's declared length.
  59. The vendor clearly did something like: you told me *prog* was 20 characters,
  60. and by golly, I'm going to use twenty characters!
  61.  
  62. That's why you should always use the "*(*)" length specifier whenever
  63. you declare a character in the argument list.
  64.  
  65. Regards,
  66. DNL
  67.  
  68.                  MY_COMMENTS = MY_OPINIONS = NOBODY_ELSES;
  69.  
  70. David N. Lombard    The first thing we do,         The MacNeal-Schwendler Corp
  71. dnl@macsch.com      Let's kill all the lawyers.    815 Colorado Blvd
  72. (213) 259-4911      II Henry VI, IV.ii             Los Angeles, CA  90041
  73.