home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.fortran
- Path: sparky!uunet!elroy.jpl.nasa.gov!nntp-server.caltech.edu!draco.macsch.com!convex.is.macsch.com!dnl
- From: dnl@convex.is.macsch.com (David Lombard)
- Subject: Re: passing a string constant to a subroutine
- Message-ID: <1992Nov20.211938.4012@draco.macsch.com>
- Sender: usenet@draco.macsch.com (Usenet Poster)
- Organization: MacNeal-Schwendler Corp.
- References: <EJH.92Nov19160031@khonshu.colorado.edu>
- Date: Fri, 20 Nov 92 21:19:38 GMT
- Lines: 61
-
- In article <EJH.92Nov19160031@khonshu.colorado.edu> 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. I
- >avoid this problem now by doing this in the calling program:
- >
- > character*20 prog
- > parameter (prog = 'atob')
- >c
- > write(*,*) 'about to call ask_data_type'
- > call ask_data_type(prog,typenum)
- >
- >But I'm just wondering if what I did was non-standard or what.
- >
- Perfectly standard, just incorrect! Your declaration:
- character*20 prog
-
- can (and was in your example) the problem. As a rule, *always*
- declare all character dummy arguments as:
- character*(*) prog
-
- As for why the *garbage*, read 15.9.3.1 (pg 15-17) from the standard:
- If a dummy argument is of type character, the associated
- actual argument must be of type character and the length of
- the dummy argument must be less than or equal to the length
- of the actual argument. If the length _len_ of a dummy
- argument of type character is less than the length of an
- associated actual argument, the leftmost _len_ characters of
- the actual argument are associated with the dummy argument.
-
- Translation: the subroutine must use a smaller length than the
- calling routine. If the subroutine's length is less, the subroutine
- cannot see the characters beyond the subroutine's declared length.
- The vendor clearly did something like: you told me *prog* was 20 characters,
- and by golly, I'm going to use twenty characters!
-
- That's why you should always use the "*(*)" length specifier whenever
- you declare a character in the argument list.
-
- Regards,
- DNL
-
- MY_COMMENTS = MY_OPINIONS = NOBODY_ELSES;
-
- David N. Lombard The first thing we do, The MacNeal-Schwendler Corp
- dnl@macsch.com Let's kill all the lawyers. 815 Colorado Blvd
- (213) 259-4911 II Henry VI, IV.ii Los Angeles, CA 90041
-