Work with pointers and dereferencing
masterCBinding.jl handles pointers to aggregate types by automatically following them through fields and array elements.
- Dereferencing: Use
ptr[]to read the value orptr[] = valto write to the memory location. - Automatic Dereferencing: Accessing a field on a pointer (e.g.,
ptr.field) returns a pointer to that field. You must then use[]to get the actual value. - Exception: Bitfields are automatically dereferenced because they cannot be referred to via a pointer.
# Assuming 'a' is an existing aggregate
ptr = Libc.malloc(a);
# ptr.i returns a Cptr, so we use [] to get the value
ptr.i[]
# Writing to a nested field via pointer
ptr.b.c[2].i[] = 42
Libc.free(ptr)ptr = Libc.malloc(a); # allocate a `struct A` as a copy of `a`
ptr.i
ptr.i[]
ptr.b.c[2].i
ptr.b.c[2].i[]
ptr.b.c[2].i[] = 42
Libc.free(ptr)