[fpc-pascal] about dynamic array

Frank Peelo f26p at eircom.net
Fri May 7 15:16:52 CEST 2010


On 07/05/2010 10:29, spir ☣ wrote:
> On Fri, 7 May 2010 06:10:30 +0200
> cobines <cobines at gmail.com> wrote:
> 
> 
>>2010/5/6 spir ☣ <denis.spir at gmail.com>:
>>
>>>(By the way, started playing with TFPList already, and could not find how to get data back! I mean the symtric of add(). Even tried indexing (who knows, with the syntactic magic of modern language? ;-).)
>>
>>It is indexing.
>>
>>var
>>  l: TFPList;
>>  p: Pointer;
>>  index: Integer;
>>begin
>>  l := TFPList.Create;
>>  index := l.Add(p);
>>  p := l[index];
>>end;
>>[...]
> 
> 
> Thank you, I must have written a typo or messed up indices, since now it works fine. Still, remains a mystery about untyped pointers, illustrated by the code below:
> 

Type
   ptInteger = ^Integer;

> var
> 	l : TFPList;
> 	i : Integer;
> 	p : ^Integer;
> begin
> 	l := TFPList.Create;
> 	i := 1 ; new(p) ; p^ := i; l.Add(p);
> 	p := l[0] ; writeln(p^);
> 	writeln(l[0]^);			// error

         writeln(ptInteger(l[0])^);

> end.

i.e. cast the untyped pointer into a pointer to integer, so the compiler 
knows what type of writeln to throw it at.

(untested)




More information about the fpc-pascal mailing list