FP106 pointer help please 
Author Message
 FP106 pointer help please

I'm using Freepas 1.0.6 in a Win32 dos box. I'm trying to do a real simple
pointer program: read lines of text from a file and assign them into a linked
list. Then print out the linked list. This program compiles but does nothing.
Could someone helpe me with this?

BTW, I did read several tutorials on linked lists on the web for FP. I'm still
having trouble understanding it. (I understood it once many years ago.)

program progname;
{uses clause here.}
uses crt,dos,keyboard,uutil{my unit};

{CONST here.}

{TYPES here.}
TYPE pItem = ^tItem;
     tItem = RECORD
                Data : STRING;
                Next : pItem;
             END;

{VARS here.}
var s,ts:string;
        i:integer;
        curitem, firstitem, tempitem:pitem;
        INFILE:text;

{*********************************************************}
{Define procedures here.}
{*********************************************************}
begin //main
Randomize;

assign(INFILE,'text.dat');
reset(INFILE);
firstitem:=nil;
new(curitem);
firstitem:=curitem; //Save pointer to first item.

while not eof(INFILE) do
        begin
        readln(INFILE,s);
        curitem^.data:=s; //Make tempitem.
        curitem^.next:=nil; //Only item does not point to anything.
        new(curitem); //Get more memory for this node.
        end; //while

close(INFILE);

//Now display list.
curitem:=firstitem; //Start at beginning.
while (curitem^.next <> nil) do
        begin
        writeln(curitem^.data);
        curitem:=curitem^.next;
        end;

end. //main

--
Freezone Freeware and Free delphi Components
http://www.*-*-*.com/ http://www.*-*-*.com/
http://www.*-*-*.com/



Mon, 02 Jan 2006 00:32:45 GMT  
 FP106 pointer help please
Hi

Quote:
> curitem^.data:=s; //Make tempitem.
> curitem^.next:=nil; //Only item does not point to anything.
> new(curitem); //Get more memory for this node.

Wrong order, at first we should allocate node, and then write to it.
Should be like this:

new(curitem); //Get more memory for this node.
curitem^.data:=s; //Make tempitem.
curitem^.next:=nil; //Only item does not point to anything.

--
Dominik Zablotny



Mon, 02 Jan 2006 01:05:37 GMT  
 FP106 pointer help please

<...>

Quote:
> assign(INFILE,'text.dat');
> reset(INFILE);
> firstitem:=nil;
> new(curitem);
> firstitem:=curitem; //Save pointer to first item.

> while not eof(INFILE) do
> begin
> readln(INFILE,s);
> curitem^.data:=s; //Make tempitem.
> curitem^.next:=nil; //Only item does not point to anything.
> new(curitem); //Get more memory for this node.
> end; //while

> close(INFILE);

1. where do you set curitem.next?
2. you will allocate one item too much

Tip: try to use one more pointer variable, e.g. "newitem"

--
Dominik Zablotny
,++,??,++,??,++,??,++,??,++,??,++,?,++,??,++
http://www.lazarus.w.pl



Mon, 02 Jan 2006 01:16:56 GMT  
 FP106 pointer help please

Quote:
> 1. where do you set curitem.next?

I meant "curitem^.next", of course.

--
Dominik Zablotny
,++,??,++,??,++,??,++,??,++,??,++,?,++,??,++
http://www.lazarus.w.pl



Mon, 02 Jan 2006 01:21:22 GMT  
 FP106 pointer help please
Thank you. I'll try it.

--
Freezone Freeware and Free Delphi Components
http://freezone.darksoft.co.nz http://chuckr.bravepages.com
http://www.bdsg.com/resources



Mon, 02 Jan 2006 03:20:15 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Pointers HELP PLEASE!

2. pointers, pointers, pointers...

3. Pointers Pointers Pointers

4. need pointer to information please

5. Pointers to pointers

6. Beginner needs some pointers with pointers!

7. POINTERS POINTERS...

8. Please Please, help me with memo-fields, real problem

9. Please, please help me with this code.

10. please, I beg of you help me please.

11. PLEASE PLEASE HELP ME

12. Please Please help me

 

 
Powered by phpBB® Forum Software