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.
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.
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.
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.
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.
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.
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.
' 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
- Safety: Always validate pointers before dereferencing.
- Scope: Be mindful of variable lifetimes when storing addresses.
- Readability: Use pointers only when necessary for clarity.
- Type Safety: Ensure pointer types match the data they reference.
- Documentation: Clearly document pointer usage in complex code.
This tutorial covered the FreeBasic @
operator with practical
examples showing its usage in different scenarios involving memory addresses.
Author
List all FreeBasic Tutorials.