Wiki source for ProPgPointers


Show raw source

{{fbdoc item="title" value="Pointers"}}----
Data types whose values are addresses in memory.

{{fbdoc item="section" value="Declaration"}}
Pointers are [[ProPgVariables|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 ##[[KeyPgInteger|Integer]] [[KeyPgPointer|Pointer]]## points to ##[[KeyPgInteger|Integer]]## data). Pointers are declared like any other variable, with the suffix "##pointer##" or "##ptr##" following the type name.

{{fbdoc item="section" value="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 ##[[KeyPgOpAt|Operator @]]## (Address of).

{{fbdoc item="section" value="Accessing pointed to data"}}
The data pointed to by a pointer can be accessed with ##[[KeyPgOpValueOf|Operator *]]## (Value of). This operator returns a reference to the data that its operand points to. The following,

{{fbdoc item="filename" value="examples/manual/proguide/pointers/builtin.bas"}}%%(freebasic)
Dim myInteger As Integer = 10
Dim myPointer As Integer Pointer = @myInteger
*myPointer = 20
Print myInteger
%%
defines an ##[[KeyPgInteger|Integer]]## variable called ##myInteger## and an ##[[KeyPgInteger|Integer]]## pointer called ##myPointer## that points to the location in memory where ##myInteger## is stored. ##[[KeyPgOpAt|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 of ##myInteger## (the expression "##*myPointer##" is the same thing as "##myInteger##").

{{fbdoc item="section" value="Pointers to user-defined types"}}
Pointers to user-defined types are defined and used like all other pointers. Accessing a member of a [[KeyPgType|Type]] or [[KeyPgClass|Class]] requires one of the following two methods:

{{fbdoc item="filename" value="examples/manual/proguide/pointers/udt.bas"}}%%(freebasic)
Type myType
a As Integer
b As Double
End Type

Dim x As myType
Dim p As myType Pointer = @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 access operator:
Print p->a
Print p->b
%%
The first method uses ##[[KeyPgOpMemberAccess|Operator . (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 method uses ##[[KeyPgOpPtrMemberAccess|Operator -> (Pointer to member access)]]##. This operator accesses members from pointers, which are automatically dereferenced. This can make code a little clearer, although both forms produce identical results.

{{fbdoc item="see"}}
- ##[[KeyPgOpAt|Operator @ (Address of)]]##
- ##[[KeyPgOpValueOf|Operator * (Value of)]]##
- ##[[KeyPgOpMemberAccess|Operator . (Member access)]]##
- ##[[KeyPgOpPtrMemberAccess|Operator -> (Pointer to member access)]]##
- ##[[KeyPgOpVarptr|Varptr]]##
- ##[[KeyPgOpStrptr|Strptr]]##
- ##[[KeyPgOpProcptr|Procptr]]##

{{fbdoc item="back" value="CatPgProgrammer|Programmer's Guide"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode