ZetCode

FreeBasic Ptr Keyword

last modified June 16, 2025

The FreeBasic Ptr keyword is used to declare pointer variables. Pointers store memory addresses of other variables or data structures.

Basic Definition

In FreeBasic, Ptr is a type specifier that creates pointer variables. Pointers are variables that hold memory addresses rather than direct values.

Pointers are essential for dynamic memory allocation, low-level memory access, and interfacing with external libraries. They provide direct access to memory locations.

Declaring Pointer Variables

This example shows how to declare and initialize pointer variables.

ptr_declare.bas
Dim x As Integer = 42
Dim p As Integer Ptr = @x

Print "Value of x: "; x
Print "Address of x: "; p
Print "Value via pointer: "; *p

Here we declare an integer variable x and a pointer to integer p. The @ operator gets the address of x. The * operator dereferences the pointer to access the value.

Pointer Arithmetic

Pointers support arithmetic operations to navigate through memory.

ptr_arithmetic.bas
Dim arr(4) As Integer = {10, 20, 30, 40, 50}
Dim p As Integer Ptr = @arr(0)

For i As Integer = 0 To 4
    Print "arr("; i; ") = "; *p
    p += 1
Next

This example demonstrates pointer arithmetic. We increment the pointer to access each array element. Pointer arithmetic automatically adjusts for the size of the pointed-to type.

Pointers and Dynamic Memory

Pointers are essential for dynamic memory allocation and deallocation.

ptr_dynamic.bas
Dim p As Integer Ptr = Allocate(SizeOf(Integer) * 5)

For i As Integer = 0 To 4
    p[i] = i * 10
Next

For i As Integer = 0 To 4
    Print p[i]
Next

Deallocate(p)

Here we allocate memory for 5 integers using Allocate. We access the memory using array notation with the pointer. Finally, we free the memory with Deallocate.

Pointers to Structures

Pointers can reference complex data types like structures.

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

Dim p1 As Person
p1.name = "Alice"
p1.age = 25

Dim p As Person Ptr = @p1

Print "Name: "; p->name
Print "Age: "; p->age

This example shows a pointer to a structure. We use the -> operator to access structure members through the pointer. This is more efficient than passing large structures by value.

Function Pointers

Pointers can also reference functions, enabling dynamic function calls.

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

Function Subtract(a As Integer, b As Integer) As Integer
    Return a - b
End Function

Dim operation As Function(a As Integer, b As Integer) As Integer Ptr
operation = @Add
Print "5 + 3 = "; operation(5, 3)

operation = @Subtract
Print "5 - 3 = "; operation(5, 3)

Here we declare a function pointer and assign it to different functions. Function pointers enable callback mechanisms and flexible program design.

Pointer to Pointer

FreeBasic supports multiple levels of indirection with pointer to pointer.

ptr_to_ptr.bas
Dim x As Integer = 42
Dim p As Integer Ptr = @x
Dim pp As Integer Ptr Ptr = @p

Print "Value of x: "; x
Print "Value via p: "; *p
Print "Value via pp: "; **pp

This demonstrates a pointer to a pointer. We use two dereference operators to access the original value. Multiple indirection is useful for modifying pointer arguments in functions.

Null Pointers

Pointers can be assigned a special null value indicating they point nowhere.

ptr_null.bas
Dim p As Integer Ptr = 0

If p = 0 Then
    Print "Pointer is null"
Else
    Print "Pointer is not null"
End If

This example checks for a null pointer. Null pointers are often used to indicate error conditions or uninitialized pointers. Dereferencing a null pointer causes runtime errors.

Best Practices

This tutorial covered the FreeBasic Ptr keyword 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.