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
--
Maurice Lombardi
Laboratoire de Spectrometrie Physique,
Universite Joseph Fourier de Grenoble, BP87
38402 Saint Martin d'Heres Cedex FRANCE
Tel: 33 (0)4 76 51 47 51
Fax: 33 (0)4 76 51 45 44
mailto:Maurice.Lombardi@ujf-grenoble.fr