L1VM - pointer
L1VM - pointer
I now added pointers to Brackets. The address of a variable can be stored in a pointer variable. So you can use the pointer on a function call and access the array:
(array Parray pointer)
The pointer variable must be of int64 type and begin with a uppercase āPā!
Here is a full example with comments. The array is printed. The first array element is changed. And then the array is printed again:
// hello-pointer.l1com
// Brackets - Hello world!
//
#include <intr.l1h>
(main func)
(set int64 1 zero 0)
(set int64 1 one 1)
(set int64 1 x 23)
(set int64 1 y 42)
(set int64 1 a 0)
(set string s hello_str "Hello world!")
(set string s addr_str "The address of string hello is: ")
(set int64 1 Pointeraddr 0)
(set int64 5 array 23 42 10 20 8)
(set const-int64 1 offset 8)
(set int64 1 Parray 0)
(set int64 1 i 0)
(set const-int64 1 array_max 5)
(set int64 1 array_ind 0)
(set int64 1 num 0)
(set int64 1 f 0)
// print string
print_s (hello_str)
print_n
((x y *) a =)
print_i (a)
print_n
print_n
// get address of variable hello:
(hello_str Pointeraddr pointer)
print_s (addr_str)
print_i (Pointeraddr)
print_n
print_n
// get array pointer
(array Parray pointer)
(for-loop)
(((i array_max <) f =) f for)
(Parray [ array_ind ] num =)
print_i (num)
print_n
((array_ind offset +) array_ind =)
((i one +) i =)
(next)
print_n
// change first array element
(y Parray [ zero ] =)
// print array again
(zero i =)
(zero array_ind =)
(for-loop)
(((i array_max <) f =) f for)
(Parray [ array_ind ] num =)
print_i (num)
print_n
((array_ind offset +) array_ind =)
((i one +) i =)
(next)
exit (zero)
(funcend)
The output is:
$ l1vm prog/hello-pointer -q
Hello world!
966
The address of string hello is: 40
23
42
10
20
8
42
42
10
20
8