Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!mtune!codas!usfvax2!pdn!alan From: alan@pdn.UUCP (Alan Lovejoy) Newsgroups: comp.lang.modula2 Subject: Re: Pointers and whatnot Message-ID: <1169@pdn.UUCP> Date: Thu, 27-Aug-87 09:53:35 EDT Article-I.D.: pdn.1169 Posted: Thu Aug 27 09:53:35 1987 Date-Received: Sat, 29-Aug-87 10:04:06 EDT References: <8708220025.AA10833@cayuga.cs.rochester.edu> <748@vixie.UUCP> <520@ivax.doc.ic.ac.uk> Reply-To: alan@pdn.UUCP (0000-Alan Lovejoy) Organization: Paradyne Corporation, Largo, Florida Lines: 43 In article <520@ivax.doc.ic.ac.uk> dcw@doc.ic.ac.uk (Duncan C White) writes: >I'm afraid this is incorrect, Paul: in Modula-2, POINTER TO CHAR is precisely >what it says: a pointer to a SINGLE character. Not a pointer to an array of >characters, or anthing else... >[suggests using ALLOCATE(pointerToChar, stringSize)] >However, doing such an operation makes little sense, since you can only >use [pointerToChar^] >which is the first character of that block of 10 characters... the remaining >nine characters can only be accessed by exceedingly messy type conversions, >which involve type casting the pointer into a pointer to some other type [an >array or record] and then accessing the fields or elements through the >fiddled pointer... The n'th byte is easily extracted by: cp := ADDRESS(pointerToChar) + n - 1; cp^; And for iterating over the string: VAR cp: POINTER TO CHAR; endCp: ADDRESS; ... cp := pointerToChar; endCp := ADDRESS(cp) + stringSize; WHILE ADDRESS(cp) <= endCp DO process(cp^); cp := ADDRESS(cp) + 1; END; This should actually be faster than using array indices, because you'll get machine code such as "MOVE.B (An),", instead of "MOVE.B 0(An,Dn)" (using m680x0 code as an example), and a good optimizer should be able to discover the "MOVE.B (An)+," optimization (combining the access with the pointer increment). Alan "String.Compare, when recoded to use this technique, ran more than twice as fast on my system" Lovejoy