🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #3767

Re: Pointers and whatnot

#3767
From: Paul Vixie Esq
Date: Sat, 22 Aug 1987 19:43
46 lines
2332 bytes
In article <8708220025.AA10833@cayuga.cs.rochester.edu> BOTCHAIR@UOGUELPH.BITNET (Alex Bewley) writes:
#TYPE PtrType = POINTER TO DataType;
#     DataType = RECORD
#                 Text : ARRAY [0..80-1] OF CHAR;
#                 next : PtrType;
#               END;
#
#VAR  Ptr : PtrType;
#
#BEGIN
#  NEW(Ptr); (* or ALLOCATE(Ptr, TSIZE(DataType); *)
#  Assign(Ptr^.Text,"This is some text...");
#END;...
#
#   In that above case, Text is 80 chars, but suppose the length of Text
#varies. How would I be able to change the type?

You could use something like

TYPE DataType = RECORD
	Text : POINTER TO CHAR;
	next : PtrType;
     END;

BEGIN
  NEW(Ptr);  ALLOCATE(Ptr.Text, strlength("what you want to put there"));
  Assign(Ptr^.Text^,...

You would have to deallocate in stages, also.  This is an old trick in C...
It ought to work fine for M2, although since arrays and pointers are not the
same thing in M2, you wound't be able to pass Ptr^.Text^ to a procedure that
was expecting ARRAY OF CHAR.  You could have the procedure expect POINTER TO
CHAR, but I'm unclear on whether you could then pass ARRAY OF CHAR params to
it without some sort of a type-cast.

#   Or suppose I just want 137 bytes of storage of no particular type, can one
#have just a POINTER with no TO?  According to my compiler, no (Logitech
#Modula-2/86).

Wirth recommends POINTER TO WORD for such things; this is supposed to be
type-compatible with all pointers.
-- 
Paul A. Vixie, Esq.		   "A viler evil than to throw a man into a
paul%vixie@uunet.uu.net		    sacrificial furnace, is to demand that he
{uunet,ptsfa,hoptoad}!vixie!paul    leap in, of his own free will, and that he
San Francisco, (415) 647-7023	    build the furnace, besides."  (Ayn Rand)
Technical Details
Message-ID: <748@vixie.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!vixie!paul
References: <8708220025.AA10833@cayuga.cs.rochester.edu>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!vixie!paul
From: paul@vixie.UUCP (Paul Vixie Esq)
Newsgroups: comp.lang.modula2
Subject: Re: Pointers and whatnot
Message-ID: <748@vixie.UUCP>
Date: Sat, 22-Aug-87 15:43:14 EDT
Article-I.D.: vixie.748
Posted: Sat Aug 22 15:43:14 1987
Date-Received: Sun, 23-Aug-87 13:10:28 EDT
References: <8708220025.AA10833@cayuga.cs.rochester.edu>
Reply-To: paul@vixie.UUCP (Paul Vixie Esq)
Distribution: world
Organization: Vixie Enterprises, San Francisco
Lines: 46