VARPTR function

Purpose

Return the 32-bit address of a variable.

Syntax

y = VARPTR(variable)

Remarks

VARPTR returns a complete 32-bit address to the specified variable as a Double-word (DWORD) value. variable is any numeric, string, structure variable (User-Defined Type or Union), or element of an array. VARPTR returns a pointer (32-bit address in memory) where the variable data is stored.

VARPTR may also be used to locate an array descriptor, as well as the array data itself.  To find the address of an array descriptor, use the array name with empty parentheses: VARPTR( x( ) ).

When you use VARPTR to get the address of a dynamic (variable length) string, keep in mind that the value being returned is the address of the string handle, not the actual data in the string.  This can be useful for manipulating a dynamic string array using indexed-pointers, For example:

DIM A$(100), b$, pA AS STRING PTR, x&

' Assume A$() is filled here

pA = VARPTR(a$(0)) ' 1st element handle

FOR X& = 0 TO 100

  B$ = B$ + @pA[x&] + ","

NEXT x&

You can use STRPTR to find the address of the string's data.  When used with pointers, VARPTR returns the address of the pointer itself.

Restrictions

VARPTR cannot be used on Register variables, because Register variables are stored in internal processor registers rather than application memory.  VARPTR can be used on UDT and Union variables, but not the UDT definition name.  For example:

TYPE MyType

   ABC AS LONG

END TYPE

' more code here

DIM x AS MyTYPE, y&

y& = VARPTR(x)      ' This is legal

y& = VARPTR(MyType) ' This is not

See also

CODEPTR, PEEK, Pointers, POKE, STRPTR

Example

DIM x AS INTEGER PTR, a%, b%

a% = 55

x = VARPTR(A%)

b% = @x

CALL DisplayResult("b% contains " + FORMAT$(b%))

Result

b% contains 55