Path: utzoo!attcan!uunet!munnari.oz.au!bunyip!brolga!uqcspe!batserver.cs.uq.oz.au!iain From: iain@batserver.cs.uq.oz.au (Iain Fogg) Newsgroups: comp.lang.pascal Subject: Re: Dynamic arrays in TP5.0 Message-ID: <4219@uqcspe.cs.uq.oz.au> Date: 10 Jul 90 00:29:58 GMT References: <1831@krafla.rhi.hi.is> Sender: news@uqcspe.cs.uq.oz.au Reply-To: iain@batserver.cs.uq.oz.au Lines: 47 binni@rhi.hi.is (Brynjolfur Thorsson) writes: >Hi there. >I am trying to use dynamic arrays in my TP5.0 program but am having a hard >time. I want to be able to do things like they are done in C, e.g. >*(x + 10) = 100.5 >but I have not found a way to do it. Below is a short program demonstrating >what I want to do, but I have no idea, how to make GetVal and PutVal. If you >have suggestions for me I would appreciate it. I can't believe that this is >impossible in TP. >*************************************************** >Program dynamic; >Var > y : ^Real; > x : Real; >Begin > GetMem(y, 10 * SizeOf(Real)); > PutVal(y, 5, 12.34); > x := GetVal(y, 3); >End. >************************************************** Try the following. Can't guarantee it's perfectly correct (haven't got TP at work), but the idea's right. Obviously, there isn't any range checking. TYPE RealPtr = ^Real; PROCEDURE PutVal (r_array: RealPtr; pos: Integer; value: Real); BEGIN r_array := RealPtr (longint (r_array) + (pos - 1) * SizeOf (Real)); r_array^ := value END; FUNCTION GetVal (r_array: RealPtr; pos: Integer): Real; BEGIN GetVal := RealPtr (longint (r_array) + (pos - 1) * SizeOf (Real))^ END; Hope it helps, Iain.