ZetCode

FreeBasic @ Operator

last modified June 16, 2025

The FreeBasic @ operator is the address-of operator that returns the memory address of a variable. It is fundamental for working with pointers and low-level memory operations in FreeBasic.

Basic Definition

In FreeBasic, the @ operator precedes a variable name and returns its memory address. This address can be stored in a pointer variable for later access or manipulation of the original variable.

The address-of operator is essential for pointer arithmetic, passing variables by reference, and interacting with external libraries that require memory addresses. It provides direct access to memory locations.

Getting Address of a Variable

This example shows how to get and display the memory address of a variable.

address_of_basic.bas
Dim number As Integer = 42
Dim numberPtr As Integer Ptr = @number

Print "Value of number: "; number
Print "Address of number: "; numberPtr
Print "Value via pointer: "; *numberPtr

Here we declare an integer variable and get its address using @. We store this address in an integer pointer. The example shows how to access both the original value and the value through the pointer.

Passing Variables by Reference

The address-of operator enables passing variables by reference to procedures.

address_of_reference.bas
Sub DoubleValue(ByRef valuePtr As Integer Ptr)
    *valuePtr *= 2
End Sub

Dim value As Integer = 5
Print "Original value: "; value

DoubleValue(@value)
Print "Doubled value: "; value

This example passes a variable's address to a subroutine that modifies the original value. The @ operator gets the address, and the subroutine uses pointer dereferencing to change the value at that address.

Array Address Operations

The address-of operator can be used with array elements to get their locations.

address_of_array.bas
Dim numbers(4) As Integer = {10, 20, 30, 40, 50}
Dim elementPtr As Integer Ptr = @numbers(2)

Print "Third element: "; numbers(2)
Print "Address of third element: "; elementPtr
Print "Value via pointer: "; *elementPtr

This demonstrates getting the address of a specific array element. The pointer can then be used to access or modify that array element directly through memory operations.

Structure Address Operations

The address-of operator works with structure variables and their members.

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

Dim user As Person
user.name = "Alice"
user.age = 30

Dim agePtr As Integer Ptr = @user.age

Print "Original age: "; user.age
*agePtr = 31
Print "Modified age: "; user.age

Here we get the address of a structure member and modify it through the pointer. This technique is useful when working with complex data structures where direct member access might be limited.

Pointer Arithmetic

Addresses obtained with @ can be used in pointer arithmetic.

address_of_arithmetic.bas
Dim values(3) As Integer = {100, 200, 300, 400}
Dim basePtr As Integer Ptr = @values(0)

For i As Integer = 0 To 3
    Print "Value "; i; ": "; *(basePtr + i)
Next

This example shows how to traverse an array using pointer arithmetic. The @ operator gets the base address, and we add offsets to access subsequent elements. This is a low-level alternative to array indexing.

Function Pointer Parameters

The address-of operator can pass variables to functions expecting pointers.

address_of_function.bas
Function SumValues(a As Integer Ptr, b As Integer Ptr) As Integer
    Return *a + *b
End Function

Dim x As Integer = 15
Dim y As Integer = 25

Print "Sum: "; SumValues(@x, @y)

Here we pass addresses of variables to a function that works with pointers. The function dereferences the pointers to access the original values. This avoids copying large data structures when passing them to functions.

Interoperability with External Libraries

The address-of operator is crucial when calling external library functions.

address_of_external.bas
' Example using Windows API function
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (hWnd As Integer, lpString As String, nMaxCount As Integer) As Integer

Dim hWnd As Integer = 12345 ' Example window handle
Dim buffer As String = Space(256)

GetWindowText(hWnd, buffer, 256)
Print "Window text: "; buffer

While this example doesn't explicitly show @, many external functions require memory addresses. The address-of operator would be used to pass variables by reference to such functions when needed.

Best Practices

This tutorial covered the FreeBasic @ operator with practical examples showing its usage in different scenarios involving memory addresses.

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.