Last active
May 7, 2021 23:04
-
-
Save kosmolot/eec7f04b9068c23460db08b7984bd387 to your computer and use it in GitHub Desktop.
A pointer to an array without copying (Vala)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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