-
Notifications
You must be signed in to change notification settings - Fork 0
Arrays
Unlike in C/C++ array types are treated essentially like structs with a ptr and a size member. (They may not be stored that way in memory, but that is just an implementation detail)
Arrays of a specific type can be declared simply by adding [] after for unsized or [n] where n is your desired size. Here's an example of how to declare arrays as variables:
let char[] x;
let char[500] y;
To access the size of an array, or even change it when unsized or dynamically allocated use the size member:
let int sz = y.size;// Sets sz to 500
To access the pointer to the data in the array use the ptr member:
let char* ptr = y.ptr;
As mentioned before you can also assign new values to these variables if they are unsized arrays or were dynamically allocated with new:
// THIS EXAMPLE LEAKS MEMORY
let array = new char[500];
array.size = 0;
array.ptr = 0;
If you try and assign to these members on either struct member arrays or fixed size stack allocated arrays the value actually never changes. Eventually it should return an error message.
// DO NOT DO THIS
let char[500] p;
p.ptr = 0;
p.size = 5;
printf("Size: %i", p.size);//prints "Size: 500"