Path: utzoo!mnetor!uunet!munnari!murdu!ucsvc!jonathan From: jonathan@ucsvc.unimelb.edu.au Newsgroups: comp.os.vms Subject: Re: pascal strings Message-ID: <240@ucsvc.unimelb.edu.au> Date: 6 May 88 12:04:35 GMT References: Organization: The University of Melbourne Lines: 116 In article , elkins@elbereth.rutgers.edu (George Elkins) writes: > Is there a way in VAX Pascal to define a procedure that will take > a variable length quoted string as a parameter? ... etc. ... > ... > Example of what I want to do: > foo('This is a string of any length'); {User-written procedure foo} > > George Elkins Various postings have been sent in response to this original query and in response to some of those followups. I may have missed something along the way, but it seems to me that the original question has not been satisfactorily answered so far. Herein, my attempt to do so - some information from previous postings may be repeated here for completeness. [This posting is rather long.] "Pascal" here refers to VAX Pascal version 3.5 running on VAX/VMS version 4.7: Pascal has two string types: - fixed length: e.g. PACKED ARRAY [1..50] OF CHAR ; - varying length: e.g. VARYING [50] OF CHAR ; Other arrays of characters can be declared but they are *not* treated as strings (e.g. ARRAY [1..50] OF CHAR). The varying length string is effectively a two-field record. For the example above, the record declaration would be like this: RECORD LENGTH : [WORD] 0..50 ; BODY : PACKED ARRAY [1..50] OF CHAR ; END ; Pascal has a number of ways to pass arrays and strings as parameters to procedures and functions. In order to answer the original question, the following method is the simplest implementation to use. Pascal generally calls this method "Conformant Schema". To declare a procedure to accept a FIXED-length READONLY string, the following syntax may be used: PROCEDURE POSSUM (String_Param : PACKED ARRAY [Low..High:INTEGER] OF CHAR) ; A variable declared as PACKED ARRAY OF CHAR of **ANY** valid length may be passed to such a procedure. Also, any non-zero length quoted string may be passed directly to such a procedure. The identifiers "Low" and "High" are implicitly declared bound identifiers and represent the lower and upper bounds respectively of the conformant array's indices. In the case of a quoted string, "High" in this example would contain a value equal to the length of the string passed. (Aside: "Low" and "High" may be any valid identifiers). Valid invocations of POSSUM would be of the form: POSSUM (Packed_String_Var) ; POSSUM ('My dog has fleas.') ; To declare a procedure to accept a VARYING-length READONLY string, the following syntax may be used: PROCEDURE WOMBAT (String_Param : VARYING [Len] OF CHAR) ; A variable declared as VARYING OF CHAR of **ANY** valid length may be passed to such a procedure. Also, any quoted string (even zero-length) may be passed directly to such a procedure. The identifier "Len" is an implicitly declared INTEGER which, in the case of a READONLY parameter, will contain the *actual* length of the string passed (NOT the maximum declared length). For a quoted string, "Len" in the example above provides the length of the string passed. (Aside: "Len" may be any valid identifier). [NOTE: If the formal parameter is declared as a *READWRITE* VARYING OF CHAR, the "Len" identifier will contain the MAXIMUM declared length of the *variable* passed.] Valid invocations of WOMBAT would be of the form: WOMBAT (Varying_String_Var) ; WOMBAT ('My cat has ferrets.') ; The length of string variables may be obtained in one of the following ways: For PACKED ARRAY OF CHAR, VARYING OF CHAR, and quoted strings, the function LENGTH (String) will return the *current* length of the string value. For VARYING OF CHAR, the pre-declared field LENGTH will return the *current* length of the VARYING array (e.g. My_Varying_String.LENGTH). When passing string parameters by descriptor to VAX/VMS Run-Time Library routines, the following conventions may normally be observed: To pass a READONLY string of *ANY* type, declare the *formal* parameter in the RTL routine declaration as a CLASS_S descriptor; e.g. Any_String : [CLASS_S] PACKED ARRAY [Lo..Hi:INTEGER] OF CHAR ; To pass a READWRITE string of type PACKED ARRAY OF CHAR, use the %STDESCR mechanism, either on the actual parameter when passed, or on the formal parameter of the RTL routine declaration; e.g. { formal declaration } %STDESCR Packed_String : PACKED ARRAY ..... ; { actual parameter } LIB$GET_COMMON (%STDESCR Return_String) ; To pass a READWRITE string of type VARYING OF CHAR, use the %DESCR mechanism, either on the actual parameter when passed, or on the formal parameter of the RTL routine declaration; e.g. { formal declaration } %DESCR Varying_String : VARYING ..... ; { actual parameter } LIB$GET_COMMON (%DESCR Return_String) ; Much of this information is available in the on-line HELP. All of it is available in the DEC publications "VAX PASCAL Reference Manual" and "VAX PASCAL User Manual". Hope all this clarifies and helps. ------------------------------------------------------------------------------- Sender: Jonathan Ridler Organization: The University of Melbourne. (University Computing Services) ACSnet,CSNET: jonathan@ucsvc.dn.mu.oz INTERNET: jonathan%ucsvc.dn.mu.oz@uunet.uu.net UUCP: {uunet,ubc-vision,nttlab,mcvax,ukc}!munnari!ucsvc.dn.mu.oz.au!jonathan -------------------------------------------------------------------------------