ZetCode

FreeBasic Dereference Operator (*)

last modified June 16, 2025

The FreeBasic * (dereference) operator accesses the value stored at a memory address pointed to by a pointer. It is fundamental for direct memory manipulation and efficient data structures.

Basic Definition

In FreeBasic, the * operator serves two purposes: declaring pointer variables and dereferencing pointers. When used as a prefix on a pointer variable, it accesses the value at the pointer's address.

Dereferencing is essential for working with dynamic memory allocation, passing arguments by reference, and implementing complex data structures. It provides low-level memory access while maintaining type safety.

Basic Dereferencing

This example shows the fundamental usage of the dereference operator.

basic_dereference.bas
Dim x As Integer = 42
Dim ptr As Integer Ptr = @x

Print "Value of x: "; x
Print "Address of x: "; ptr
Print "Value at ptr: "; *ptr

Here we create an integer variable x and a pointer to it. The @ operator gets x's address. The *ptr dereferences the pointer to access x's value. This demonstrates direct memory access through pointers.

Modifying Through Dereference

Dereferencing allows modifying the value at a pointer's address.

modify_dereference.bas
Dim value As Integer = 10
Dim pValue As Integer Ptr = @value

Print "Original value: "; value
*pValue = 20
Print "Modified value: "; value

This code modifies value indirectly through its pointer. The *pValue = 20 line changes the value at pValue's address. This shows how dereferencing enables indirect variable modification.

Pointer to Pointer

Dereferencing works with multiple levels of indirection.

pointer_to_pointer.bas
Dim num As Integer = 100
Dim pNum As Integer Ptr = @num
Dim ppNum As Integer Ptr Ptr = @pNum

Print "num: "; num
Print "*pNum: "; *pNum
Print "**ppNum: "; **ppNum

Here we create a pointer to a pointer. The double dereference **ppNum first gets pNum, then dereferences that to get num. This demonstrates multi-level pointer dereferencing in FreeBasic.

Dereferencing Structure Pointers

The dereference operator works with structure pointers.

struct_dereference.bas
Type Person
    name As String
    age As Integer
End Type

Dim p As Person
Dim pPtr As Person Ptr = @p

*pPtr = Type("Alice", 30)
Print "Name: "; (*pPtr).name
Print "Age: "; (*pPtr).age

This example shows dereferencing a structure pointer. We use parentheses around *pPtr to properly scope the dereference before accessing members. The arrow operator -> is often preferred for this.

Arrow Operator vs Dereference

The arrow operator combines dereference and member access.

arrow_operator.bas
Type Point
    x As Integer
    y As Integer
End Type

Dim pt As Point
Dim pPt As Point Ptr = @pt

pPt->x = 10
pPt->y = 20

Print "Point: ("; pPt->x; ", "; pPt->y; ")"
Print "Equivalent to: ("; (*pPt).x; ", "; (*pPt).y; ")"

The arrow operator -> is syntactic sugar for (*ptr).member. It makes structure pointer access cleaner. Both forms are shown here for comparison. The arrow version is generally preferred for readability.

Dereferencing Array Pointers

Array elements can be accessed through pointer dereferencing.

array_dereference.bas
Dim arr(3) As Integer = {10, 20, 30, 40}
Dim pArr As Integer Ptr = @arr(0)

For i As Integer = 0 To 3
    Print "arr["; i; "] = "; *(pArr + i)
Next

This demonstrates array access through pointer arithmetic and dereferencing. *(pArr + i) is equivalent to arr(i). Pointer arithmetic automatically scales by the type size. This shows low-level array access.

Dereferencing Function Pointers

Function pointers can be dereferenced to call functions.

function_pointer.bas
Function Add(a As Integer, b As Integer) As Integer
    Return a + b
End Function

Dim pFunc As Function(a As Integer, b As Integer) As Integer Ptr = @Add

Print "5 + 7 = "; (*pFunc)(5, 7)

Here we create a pointer to a function and call it through dereferencing. The (*pFunc)(5, 7) syntax calls the function pointed to by pFunc. This demonstrates how function pointers enable dynamic dispatch.

Best Practices

This tutorial covered the FreeBasic dereference operator with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.