Basic array usage

For this example we add stdlib.h, stdio.h and string.h for some convenience functions. The first thing to do to be able to use an Eina_Array is to include Eina.h:

#include <stdio.h>
#include <string.h>
#include <Eina.h>

Here we have a callback that prints the element given to it:

static Eina_Bool
_print(const void *container EINA_UNUSED, void *data, void *fdata EINA_UNUSED)
{
printf("%s\n", (char *)data);
return EINA_TRUE;
}

Now we create our entry point and declare some variables, nothing special:

int
main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
const char* strings[] = {
"helo", "hera", "starbuck", "kat", "boomer",
"hotdog", "longshot", "jammer", "crashdown", "hardball",
"duck", "racetrack", "apolo", "husker", "freaker",
"skulls", "bulldog", "flat top", "hammerhead", "gonzo"
};
Eina_Array *array;
unsigned int i;

Before we can start using any array function we need to initialize eina:

So now to actually create our array. The interesting thing here is the argument given to the eina_array_new() function. This argument sets how fast the array grows.

array = eina_array_new(10);

If you know before hand how big the array will need to be you should set the step to that. In our case we can set it to the number of strings we have and since we didn't do that in the eina_array_new() we can do it now:

eina_array_step_set(array, sizeof(*array), 20);

Now let us populate our array with some strings:

for (i = 0; i < 20; i++)
eina_array_push(array, strdup(strings[i]));
Note
Notice we use strdup, so we will have to free that memory later on.

Now lets check the size of the array:

printf("array count: %d\n", eina_array_count(array));

And now we call a function on every member of our array to print it:

eina_array_foreach(array, _print, NULL);

One of the strengths of Eina_Array over Eina_List is that it has very fast random access to elements, so this is very efficient:

printf("Top gun: %s\n", (char*)eina_array_data_get(array, 2));

And now we free up the memory allocated with the strdup()s:

while (eina_array_count(array))
free(eina_array_pop(array));

And the array memory itself:

And finally shutdown eina and exit:

return 0;
}

The full source code can be found in the examples folder in the eina_array_01.c file.

eina_array_step_set
EAPI void eina_array_step_set(Eina_Array *array, unsigned int sizeof_eina_array, unsigned int step)
Sets the step of an array.
Definition: eina_array.c:305
EINA_UNUSED
#define EINA_UNUSED
Definition: eina_types.h:321
Eina.h
Eina Utility library.
eina_array_foreach
static Eina_Bool eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
Iterates over an array using a callback function.
eina_array_count
static unsigned int eina_array_count(const Eina_Array *array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT
Returns the number of elements in an array.
eina_init
int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:277
_Eina_Array
Definition: eina_array.h:228
eina_array_free
EAPI void eina_array_free(Eina_Array *array)
Frees an array.
Definition: eina_array.c:295
eina_array_push
EAPI Eina_Bool static Eina_Bool eina_array_push(Eina_Array *array, const void *data) EINA_ARG_NONNULL(1
Appends a data item to an array.
eina_shutdown
int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:348
EINA_TRUE
#define EINA_TRUE
Definition: eina_types.h:508
eina_array_new
EAPI Eina_Array * eina_array_new(unsigned int step)
Creates a new array.
Definition: eina_array.c:276
eina_array_pop
EAPI Eina_Bool static Eina_Bool static void * eina_array_pop(Eina_Array *array) EINA_ARG_NONNULL(1)
Removes the last data item in an array.
Eina_Bool
unsigned char Eina_Bool
Definition: eina_types.h:496
eina_array_data_get
static void * eina_array_data_get(const Eina_Array *array, unsigned int idx) EINA_ARG_NONNULL(1)
Returns the data at a given position in an array.