Pointer Variable
A pointer can point to a variable, an array of variables, a structure, an array of structures, or a user-defined function.
Syntax
Dim ptr as long
Ptr = @MyVariable
OR
MyVariable = !ptr

Example #1
This example is a simple program that shows the basic functionality of the pointers.
Public Trial1, Trial2, Trial3 As String
Public Trial4, Trial5
Public Trial6 As Long, Trial7
Public Trial8
'Declare pointer
Dim MyPointer As Long 'Pointer must be declared as Type Long
'Function that returns a pointer; must be Type Long
Function MyFunction1 (A As Long) As Long
Return(A) 'return a pointer
EndFunction
'Function that returns the value of a pointer
Function MyFunction2 (A As Long)
Return(!A)
EndFunction
'Begin Main Program
BeginProg
'Example 1
Trial1 = 345 'Set Value for Trial1
MyPointer = @Trial1 'pointer is set to the memory address of Trial1
Trial2 = !MyPointer 'Trial2 is set to the value at the pointer address
Trial3 = !(MyPointer) 'This will return the variable name associated with the address
'Example 2
MyPointer = @Trial4 'Pointer is set to the memory address of Trial 4
Trial5 = 123 'Set Value for Trial5
!MyPointer = Trial5 'The value at the pointer Address is set to the value of Trial5
'Example 3
Trial6 = MyFunction1(@Trial1) 'Set Trial6 to the memory address returned by the function
Trial7 = !Trial6 'Set trial7 to the dereferenced value of trial6.
'Example 4
Trial8 = MyFunction2(@Trial1) 'Set Trial8 to the value returned by the function
EndProg
Example #2
This example demonstrates using a pointer to reference user-created functions.
A pointer to a function behaves the same as referencing the function directly.
If the parentheses for the parameters are present, the function is called.
(!ptr)()
If the parentheses for the parameters are not present, the pointer returns the value that the function returned when called last.
(!ptr)
'This example demonstrates using a pointer to reference user-created functions.
Function f1(f)
Return f
EndFunction
Function f2
Return 2.2
EndFunction
BeginProg
Scan (1,Sec,0,0)
Dim ptr1 As f1!, ptr2 As f2!
ptr1 = @f1
Public v1,v2
v1 = (!ptr1)(1.1)
ptr2 = @f2
v2 = (!ptr2)()
NextScan
EndProg
Example #3
'This example demonstrates using a pointer to reference a structure.
'set up structure type
StructureType s
value As Float
flag As Boolean
EndStructureType
Const size = 10
'declare structures as type s
Public s1(size) As s, s2(size) As s, input As s
BeginProg
Scan (1,Sec,0,0)
'note: structure.structure-variable is analogous to tablename.fieldname syntax
input.value = Public.Record
input.flag = Public.Record MOD 2
Dim i As Long
'add input to the array, throwing out the oldest
For i = 1 To size-1
s1(i) = s1(i+1)
Next i
s1(size) = input
'use pointers to reference a structure
Dim ptr3 As s!, ptr4 As s!
ptr3 = @s1 : ptr4 = @s2
For i = 1 To size
!ptr4 = !ptr3
ptr4 += 1 : ptr3 += 1
Next i
NextScan
EndProg
Remarks
A pointer can point to a variable, an array of variables, a structure, an array of structures, or a user-defined function. Pointers are a convenient way to reference the memory location of a variable in a program rather than referencing it by name. Pointers are useful in functions where parameters are local to the function and changes to the parameters have no effect on the original arguments.
Pointer variables must be initialized by the @ operator before they can be used. E.g.,
PTR as Long!
PTR = @X
Reference to an uninitialized pointer will return a variable out of bounds error.
In most applications, pointer variables are declared as Type Long! However, pointer variables may also be declared as type Float!, Boolean!, or String! (for example, Dim P as Float!).
@ - The "@" symbol is used to reference the memory location of a variable in a program
! – The "!" symbol is used to dereference a pointer. This will return the value at the pointer rather than the memory location of the pointer.
! (VariableName) – The "()" around the variable name will return the variable name associated with the memory location. This must be declared as type string.
Pointers can also be used with Functions (see Example Program).
!Pointer(X)
This will access the xth element of an array of pointers.
The ! operator can also be applied to a user function call, when the function returns a pointer. For example:
Function ConstrainFunc(Value As Long,Low As Long,High As Long) As Long
If !Value < !Low Then
Return Low
ElseIf !Value > !High Then
Return High
Else
Return Value
EndIf
EndFunction
‘Call within program
FuncFltRes = !ConstrainFunc(@FltVal,@FltLow,@FltHigh)
A pointer referenced inside an expression can also be cast as a specific type (e.g., !(FLOAT!)(p+1) = !(FLOAT!)(p-3) + !(float!)(p-2) ).
Due to how memory is managed in the datalogger, there cannot be more than 4094 variable declarations before declaring a variable that will be accessed via a pointer. Additionally, the size of an array that can be accessed via a pointer is limited to 1,048,576.
Pointers can reference values in data tables by using the TableName.Fieldname syntax. This allows both writing and reading via a pointer reference to a data table.