This example will create an inline array of chars, add some elements, print it, re-purpose the array to store ints, add some elements and print that.
We'll start with a function to compare ints we need this because the '>' operator is not a function and can't be used where Eina_Compare_Cb is needed.
And then move on to the code we actually care about, starting with variable declarations and eina initialization:
Creating an inline array is very simple, we just need to know what type we want to store:
- Note
- The second parameter(the step) is left at zero which means that eina will choose an appropriate value, this should only be changed if it's known, beforehand, how many elements the array will have.
Once we have an array we can start adding elements to it. Because the insertion function expect a memory address we have to put the value we want to store in a variable(this should be no problem since in real world usage that's usually where the value will be anyways):
- Note
- Because the inline array copies the value given to it we can later change
ch
, which we do, without affecting the contents of the array.
So let's add some more elements:
We will then iterate over our array and print every position of it. The thing to note here is not so much the values which will be the expected 'a', 'b', 'c' and 'd', but rather the memory address of these values, they are sequential:
We'll now use our array to store ints, so we need to first erase every member currently on the array:
And then to be able to store a different type on the same array we use the eina_inarray_step_set() function, which is just like the eina_inarray_new() function except it receives already allocated memory. This time we're going to ask eina to use a step of size 4 because that's how many elements we'll be putting on the array:
- Note
- Strictly speaking the reason to call eina_inarray_setup() is not because we're storing different type, but rather because our types have different sizes. Eina inline arrays don't actually know anything about types, they only deal in blocks of memory of a given size.
-
Since eina_inarray_step_set() receives already allocated memory you can(and it is in fact good practice) use inline arrays not declared as pointers: Eina_Inarray arr;
And now to add our integer values to the array:
Just to change things up a bit we've left out the 99 value, but will still add it in such a way to keep the array ordered. There are many ways to do this, we could use eina_inarray_insert_at(), or we could change the value of the last member using eina_inarray_replace_at() and then append the values in the right order, but for no particular reason we're going to use eina_inarray_insert_sorted() instead:
We then print the size of our array, and the array itself, much like last time the values are not surprising, and neither should it be that the memory addresses are contiguous:
Once done we free our array and shutdown eina:
The source for this example: eina_inarray_01.c