Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo Pascal - <b> pointer define and access pp 119</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 POINTER                      Define and Access                       pp 119


 Define:  A pointer is a 32 bit Segment:Offset of a variable in memory.

 Purpose: Pointers are used to access dynamic variables.  These variables
          are created at run time in the Heap space.  When the variable is
          created, a pointer is set with the Segment:Offset address of the
          variable.  Always initialize a pointer to Nil at startup.

 Notes:   A pointer is a Typed variable which is defined in the declaration
          section of the source code.  Until it is set at run time, it has
          no value assigned to it.  It is just a address in memory with
          space assigned for 32 bits of data.

          A pointer must have a type.  It can be any of the pre-defined
          types such as Integer, Real, String, Byte, Char.



 Simple Types:

       TYPE
          StrTyp   = String [80] ;     { Specific length string type }
       VAR
          IntPtr   : ^Integer    ;     { Pointer to Integer variable }
          StrPtr   : ^StrTyp     ;     { Pointer to String[80] variable }
          RealPtr  : ^Real       ;     { Pointer to Real variable }
          BytePtr  : ^Byte       ;     { Pointer to Byte variable }
          CharPtr  : ^Char       ;     { Pointer to Char variable }

       BEGIN
          IntPtr^  := 14         ;     { Assign integer value }
          StrPtr^  := 'abcde'    ;     { Assign up to 80 string bytes }
          RealPtr^ := Pi         ;     { Assign pre-defined Pi real value }
          BytePtr^ := 14         ;     { Assign byte value }
          CharPtr^ := 'A'        ;     { Assign character value }
       END.


 Record Types:

       TYPE
          NodePtr  = ^Node           ; { User defined type }
          Node     =  Record
                      Prev : NodePtr ; { Previous record pointer }
                      Next : NodePtr ; { Next record pointer }
                      End ;

       VAR
          FirstRec : NodePtr ;         { First record of double link list }
          LastRec  : NodePtr ;         { Last record of double link list }
          NewRec   : NodePtr ;         { New allocated record }

       Procedure FirstRecord ;
          Begin
          New (FirstRec) ;             { Create first record in heap space }
          With FirstRec^ Do            { Initialize 1st record data }
            Begin
            Prev := FirstRec ;         { Previous pointer = FirstRec pointer }
            Next := FirstRec ;         { Circle back to FirstRec at list end }
            End ;
          LastRec := FirstRec ;        { Last record ptr = FirstRec pointer }
          End ;

       Procedure NewRecord ;
          Begin
          New (NewRec) ;               { Create new record at NewRec }
          LastRec^.Next := NewRec   ;  { Point to this record }
          NewRec^.Prev  := LastRec  ;  { Point to previous record }
          NewRec^.Next  := FirstRec ;  { Circle back to FirstRec }
          LastRec       := NewRec   ;  { New record is now last record }
          End;

       BEGIN
          FirstRecord ;                { Allocate the first record }
          NewRecord ;                  { Allocate new record }
          NewRecord ;                  { Allocate new record }
       END.

See Also: Dispose Heap Mark New Nil Ptr Releas

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson