Pointer
 

      Pointer in is not a command. It's a keyword used to explicitly declare an item as being that of Pointer data type.




FACTS:


      * Note: PlayBASIC V1.64 supports the following pointer types, Generic, Byte, Word, Integer, Float and UserDefinedTypes (Types)

      * To Read & Write to pointer, use the '*' symbol as a prefix. This denotes that you want to get the information a pointer is pointing at, and not the pointer address.

      * Arithmetic between Byte/Word/Integer/Float Pointers (ie Address=Address+1) will automatically scale the value to the Pointers data width.

      * When mixing pointer types, PlayBASIC expects the programmer to explicitly cast the terms.

      * When writing to a generic pointer, the data type is derived from the data type of the expression result.

      * When Reading from a generic pointer, the user has to explicitly cast the read datatype

      * Important: Attempting to read/write from a pointer that has yet to be initialized (told what it should point at) will most certainly crash your application ! So care is definitively needed when dealing with pointers!


Ie.
  
  Dim Address As Pointer
; recast the generic as Integer pointer and read then long from memory
  Print *IntPtr(Address)
  


* Typed Pointers (User defined type pointers) allow you to append the user defined types fields to Read/Write pointer accesses.




General Pointer Examples:

      This example shows how to declare the various Pointer types. See the Byte,Word,Integer,Float for usage examples


Pointer Declaration Example

  
; *=----------------------------------------=*
; Explicitly Delcare POinter types using DIM
; *=----------------------------------------=*
  
; Declare a GENERIC Pointer (a pointer to read/write
; byte/word/integer/float values)
  Dim MyGenericPointer As Pointer
  
  
; Declare a Byte Pointer (a pointer to read/write byte values)
  Dim MyBytePointer As Integer Pointer
  
  
; Declare a Word Pointer (a pointer to read/write
; Word values)
  Dim MyWordPointer As Word Pointer
  
  
; Declare a Integer Pointer (a pointer to read/write;
; Integer values)
  Dim MyIntegerPointer As Integer Pointer
  
  
; Declare a Float Pointer (a pointer to read/write
; Float values)
  Dim MyFloatPointer As Float Pointer
  




Pointer User Defined Types:

      This example shows how you can declare a user define type pointer and how to use it feilds.


Example
  
  
` Declare a Userd defined type called tVertex
  Type tVertex
     x#,y#,z#
  EndType
  
; Each Type is 12 bytes (3 floats 3*4=12)
  SizeOf_tVertex=12
  
; declare a User Defined type pointer
  Dim MyVertexPointer As tVertex Pointer
  
; Create a bank
  CreateBank 1,1000
  
; Use this pointer to write some Integer values to a bank
  For lp=0 To 10
   ; Set pointer address of this item
     MyVertexPointer=GetBankPtr(1)+(lp*SizeOf_tVertex)
     MyVertexPointer.x#     = 100.0+lp
     MyVertexPointer.Y#     = 200.0+lp
     MyVertexPointer.Z#     = 300.0+lp
  Next
  
; Use this pointer to read the values back
  For lp=0 To 10
     MyVertexPointer=GetBankPtr(1)+(lp*SizeOf_tVertex)
     Print MyVertexPointer.x#
     Print MyVertexPointer.y#
     Print MyVertexPointer.Z#
  Next
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  




This example would output.

  
  100.0
  200.0
  300.0
  101.0
  201.0
  301.0
  102.0
  202.0
  302.0
  103.0
  203.0
  303.0
  104.0
  204.0
  304.0
  105.0
  205.0
  305.0
  106.0
  206.0
  306.0
  107.0
  207.0
  307.0
  108.0
  208.0
  308.0
  109.0
  209.0
  309.0
  110.0
  210.0
  310.0
  
  


 
Related Info: Byte | Dim | Explicit | Float | Global | Integer | Local | Static | String | Type | Types | Word :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com