Contents | Index | < Browse | Browse >
2.7 Pointers

  A pointer stores a memory address. Technically, on 32bit machines, this is a
  32bit value.
  To declare a pointer variable to a certain type instead of the type
  itself, the "*" (asterix) sign will be put in front and becomes part
  of the name, thus has always to be written.
  If the content of the pointer is addressed, it needs to be dereferred by
  accessing the fields in the Newtype the pointer is pointing to.
  Primitive types cannot access their values, only via the Peek.t() function.  

  Example:
  +-----------------------------------------------------------------------------
  | 
  | *myPointer.l = 0     ; declare *myPointer and let it point to address 0
  | 
  | *myPointer = 12345   ; this would change the pointer to point to 12345
  |                      ; (which is a bad idea, actually)
  |  
  +-----------------------------------------------------------------------------
  
2.7.1 Dereferencing a Pointer

  To get the content a pointer points to, the type must be a newtype. It is
  dereferenced by accessing the fields in the newtype, just as if it was a
  "real" newtype rather than a pointer.
  Pointers to primitve types cannot be dereferenced. You need the function
  "Peek.t" to access the content.
  
  Example:
  +-----------------------------------------------------------------------------
  | 
  | NEWTYPE.t
  |   x.l
  | End NEWTYPE
  |
  | DEFTYPE.l dummy
  | *myNewtype.t = &dummy.l ; let *myNewtype point to the content of dummy
  | *myNewtype\x = 12345    ; this will set the content of *myNewtype\x to
  |                         ; 12345.
  |                         ; Note: because it points to the content of dummy,
  |                         ; dummy is changed too!
  | 
  | *myLong.l = &dummy.l    ; let *myLong point to the content of dummy
  |                         ; there is actually no way to access the content.
  | y.l = Peek.l(*myLong)   ; ...unless you use Peek.t()
  | Poke.l *myLong,12345    ; ...and Poke.t
  |
  +-----------------------------------------------------------------------------  
  Note: See next subsection what the "&" is doing!
  
2.7.2 Referencing a Variable
 
  To get a pointer to a variable use "&" (ampersand) in front of its name.

  Example:
  +-----------------------------------------------------------------------------
  | 
  | DEFTYPE.l dummy  
  | *myLong.l = &dummy.l  ; let *myLong point to the content of dummy
  | 
  +-----------------------------------------------------------------------------

  Warning: Be careful with pointers. This is for advanced programming.
  Because AmigaOS has no memory protection, you can crash your machine easily
  if the pointer points to a wrong/orphaned place.
  

Contents | Index | < Browse | Browse >