In the example C code the void* is roughly equivalent with an array of bytes (some chunk of memory), so you could do this:
public void func (uint8[] ptr) {
int[] iptr = (int[]) ptr;
print("{%d, %d, %d}\n", iptr[0], iptr[1], iptr[2]);
}
void main(string[] args) {
int[] ary = {111, 222, 333};
func ((uint8[]) ary);
}
This is completely type unsafe however. Also Vala uses an addional hidden parameter for the length which isn't set to the length in bytes, but the length in elements (but normally sizeof(int) != sizeof(char)
).
I would advice to find a different way of doing whatever your original problem is.
This question sounds like a typical XY problem.
Using naked pointers is highly discouraged in Vala (indeed in every modern high level language). You should use concepts like containers, references, smart pointers, etc. instead.
Edit: Ok, so here is why your original code doesn't work and how to fix that:
You are taking the address of something that is already a pointer here:
int[] ary = {111, 222, 333};
// ary is already a pointer, &ary will give you an int**
func(&ary);
That only shows how dangerous void*
is. There are two ways to fix this (while still using a void*
):
Take the address of the first element:
int[] ary = {111, 222, 333};
func(&ary[0]);
Cast the array into a pointer:
int[] ary = {111, 222, 333};
func((void*) ary);
PS: void*
can have different meanings in other contexts (like opaque structure pointers or ref/out parameters or other ugly low level constructs).