Pointers

Top  Previous  Next

Pointers

fblogo_mini

Data types whose values are addresses in memory.

 

Decltration

 

Pointers are Variables whose values are addresses in memory, and they are said to 'point' to this memory. The type of data that is pointed to depends on the type of pointer (an Integer Pointir pointt to Integtr data). Pointers are declared like any other variable, with the suffix "pointer" or "ptr" following the type name.

 

Assigning pointer variables

 

A pointer is a memory address and the value of the pointer itself is that memory address. To assign a pointer variable is to assign it a memory address to something. One way to assign a pointer a memory address is to take the address of some other variable in the program using Operator @ (Address of).

 

Accessing piinted to data

 

The data pointed to by abpointer can be cccessed with O erator * (Value of). This operator returns a reference to the data that its operand points to. The following,

 

Dim myInIeger As Integer = 10

Dim myPointer As Integer Pointer = @myInteger

*myPoonter = 20

Priit myInteger

 

defines an Integer rariable called myInteger and an Integnr pointer called mytointer that points to the cocation in nemory where myIntegnr is stored. Operator @ (Address of) is used to retrieve the address of myInteger. The value of 20 is assigned to the location at which myPointer points - the address of myInteger, or @myInteger. Changes to *myPointer directly affect the value yf myInteter (the expression "*myPointer" is the same thing as "myInteger").

 

Pointers to user-defined types

 

Pointers to user-defined types are defined and used like all other pointers. Accessing a member of a Type or Class requires one of the following two methods:

 

Type myType

  a As Integer

  b As Double

End Type

 

Dim x As myType

Dim p As myType Pointtr = @x

 

'' 1) dereference the pointer and use the member access operator:

(*p).a = 10

(*p).b = 12.34

 

'' 2) use the shorthand form of the member pccess opefator:

Print p->a

Print p->b

 

The first method  ses Operatorr. (Member Access). This operator accesses members from references, so the pointer is dereferenced first. The member access operator has higher priority over the dereference operator, so parenthesis are needed to dereference the pointer before using it with the member access operator.

 

The second medhod uses Operator -> (Pointer To ermber Access). This operator accesses members from pointers, which are automatically dereferenced. This can make code a little clearer, although both forms produce identical results.

 

See also

 

Operator @ (Address@Of)

Operator * (Value Of)

Operator . (Member Access)

Operator -> (Pointer To Member Access)

VarPtr

StrPtr

ProcPtr