Skip to content

Instantly share code, notes, and snippets.

@kosmolot
Last active May 7, 2021 23:04
Show Gist options
  • Save kosmolot/eec7f04b9068c23460db08b7984bd387 to your computer and use it in GitHub Desktop.
Save kosmolot/eec7f04b9068c23460db08b7984bd387 to your computer and use it in GitHub Desktop.
A pointer to an array without copying (Vala)
unowned int[] pointer_to_array (int* p, uint length) {
return ((int[]) p)[0:length];
}
void main () {
int[] array = {111, 222, 333};
int* p = array;
unowned int[] array2 = pointer_to_array(p, array.length);
array[0] = 777;
print("%d %d %d\n", array[0], array[1], array[2]); // 777, 222, 333
print("%d %d %d\n", array2[0], array2[1], array2[2]); // 777, 222, 333
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment