Language3 min read

Pointers

Pointer types, address-of operator, and dereferencing in BWSL.

Reading Time
3 min
Word Count
409
Sections
13
Try It Live

Pressure-test the syntax

Take the concept from this page into the playground and deliberately break a pass, binding, or type signature to see how the compiler responds.

Try a Live Edit

BWSL provides pointer types for indirect access to memory locations. Unlike traditional C-style pointers, BWSL uses a postfix ^ syntax that keeps pointer operations distinct from bitwise XOR.

Pointer Declaration

Declare a pointer type by appending ^ to the base type:

TypeDescription
int^Pointer to int
float^Pointer to float
uint^Pointer to unsigned int
bool^Pointer to bool
bwsl
int x = 42;
int^ ptr = ^x; // ptr points to x

Address-Of Operator

Use the prefix ^ operator to get the address of a variable:

bwsl
int value = 100;
int^ pointer = ^value; // Get address of 'value'
float f = 3.14;
float^ fptr = ^f; // Get address of 'f'

The address-of operator creates a pointer that references the original variable's memory location.

Dereference Operator

Use the postfix ^ operator to access the value at a pointer's address:

bwsl
int x = 42;
int^ ptr = ^x;
// Read through pointer
int readValue = ptr^; // readValue is 42
// Write through pointer
ptr^ = 100; // x is now 100

Notice that ^ is used as a prefix for address-of (^x) and as a postfix for dereference (ptr^). This keeps the syntax consistent: the ^ is always adjacent to what it operates on.

Reading and Writing

Pointers enable indirect reads and writes to variables:

bwsl
int counter = 0;
int^ counterPtr = ^counter;
// Read-modify-write through pointer
counterPtr^ = counterPtr^ + 1; // Increment through pointer
counterPtr^ = counterPtr^ + 1;
counterPtr^ = counterPtr^ + 1;
int finalCount = counter; // finalCount is 3

This is equivalent to directly modifying counter, but allows the modification to happen indirectly.

Pointer Reassignment

Pointers can be reassigned to point to different variables:

bwsl
int a = 10;
int b = 20;
int^ p = ^a;
int fromA = p^; // fromA is 10
p = ^b; // Reassign pointer to b
int fromB = p^; // fromB is 20

Ensure the pointer is reassigned to a variable of the same type. Assigning a pointer to an incompatible type will result in a compiler error.

Pointers in Expressions

Dereferenced pointers can be used anywhere a value is expected:

bwsl
int val = 50;
int^ valPtr = ^val;
// Arithmetic with dereferenced values
int sum = valPtr^ + 10; // 60
int diff = valPtr^ - 5; // 45
int prod = valPtr^ * 2; // 100
// Comparison
bool isPositive = valPtr^ > 0;

Pointers vs Bitwise XOR

BWSL uses ^ for both pointers and bitwise XOR, but the context makes the meaning unambiguous:

bwsl
int xorA = 0b1010;
int xorB = 0b1100;
// Bitwise XOR - binary operator between two values
int xorResult = xorA ^ xorB; // 0b0110 = 6
// Address-of - prefix unary operator
int value = 42;
int^ ptr = ^value; // Pointer to value
// Dereference - postfix unary operator
int read = ptr^; // Read through pointer
ExpressionMeaning
a ^ bBitwise XOR of a and b
^xAddress of variable x
ptr^Dereference pointer ptr

When in doubt, remember: prefix ^ takes an address, postfix ^ dereferences, and infix ^ is XOR.

Type Compatibility

Pointer types must match their target types exactly:

bwsl
int x = 10;
float f = 3.14;
int^ intPtr = ^x; // Valid
float^ floatPtr = ^f; // Valid
// int^ wrongPtr = ^f; // Error: cannot assign float^ to int^

Common Patterns

Swap Values

Use pointers to swap values through a helper function:

bwsl
swap :: (int^ a, int^ b) -> void {
int temp = a^;
a^ = b^;
b^ = temp;
}
int x = 10;
int y = 20;
swap(^x, ^y); // x is now 20, y is now 10

Accumulator Pattern

Pass a pointer to accumulate results:

bwsl
accumulate :: (int^ sum, int value) -> void {
sum^ = sum^ + value;
}
int total = 0;
accumulate(^total, 5);
accumulate(^total, 10);
accumulate(^total, 15);
// total is now 30

Pointer Summary

SyntaxNameDescription
Type^Pointer typeDeclares a pointer to Type
^variableAddress-ofGets the address of a variable
pointer^DereferenceAccesses the value at the pointer's address
ptr^ = valueWrite through pointerAssigns a value to the pointed location
ptr = ^otherPointer reassignmentPoints to a different variable

See Also