Nicola Girardi a écrit :
Hi fine GPC hackers, please take a look at this foolish code, I, as a novice, have a little problem with pointers in Pascal ;-) (I wonder why I don't just keep using C:))
: type : PStrList = ^TStrList; : TStrList = record : Next : PStrList; : S : TString; : end; : : var : StringList : PStrList; : : procedure Foobar; : var : Str : PStrList = StringList; : begin : repeat : New(Str); : Readln(F (* : Text *), Str^.S); : if Length(Hdr^.S) = 0 then : break; : Str := Str^.Next;
Here Str^.next contains only garbage and you put this garbage into Str...
: until False; : end;
Due to the assignment of StringList to Str, at the end of Foobar I'd get StringList not to be the head of the list but the last element. How can I keep a pointer which always points to the first element? Maybe that's really easily done, but of course in Pascal it doesn't seem that it works like in C, and I'm out of manuals ;-)
Look to iteratordemo.pas in the demo programs which come with gpc, especially the procedure ReadList which does exactly what you want. You need an extra level of indirection.
Maurice