-
Notifications
You must be signed in to change notification settings - Fork 3
Pointers
Hassium has the concept of pointers, similar to languages such as C, C++, and C#. A pointer is an object type that holds the variable in question and the stack frame in which it was created. It allows for a variable to be passed around, assigned to, and the value will be changed in any function which holds the pointer.
A pointer can be created from any variable using the & reference unary operator. This will return a pointer object that can then be passed around. An example would be as follows:
func main () {
a = 45;
b = &a;
}
a
still holds the value 45
, but b
is now of type pointer and holds a reference to a
.
A pointer object can be dereferenced, meaning the value be accessed and returned from the pointer so that it can be examined. This uses the * dereference unary operator like so:
func main () {
a = 45;
b = &a;
test ();
}
func test (p) {
println (*p); # *p accesses the pointer that was passed to test () and retrieves the value of a.
}
The value referenced by the pointer can be modified once again by using the dereference operator on the left hand side of the assignment operator. Here is an example:
func main () {
a = 45;
b = &a;
test (b);
println (a); # Prints 46.
}
func test (p) {
*p = 46; # This assigns the value 46 to the variable referenced by the pointer.
}