1tutorial_array_page(3) Library Functions Manual tutorial_array_page(3)
2
3
4
6 tutorial_array_page - Array Tutorial The Array data type is allow the
7 storage of data like a C array.
8
9 It is designed such that the access to its element is very fast. But
10 the addition or removal can be done only at the end of the array. To
11 add or remove an element at any location, the Eina List is the correct
12 container is the correct one.
13
15 An array must created with eina_array_new(). That function takes an
16 integer as parameter, which is the count of pointers to add when
17 increasing the array size. Once the array is not used anymore, it must
18 be destroyed with eina_array_free().
19
20 To append data at the end of the array, the function eina_array_push()
21 must be used. To remove the data at the end of the array,
22 eina_array_pop() must be used. Once the array is filled, one can check
23 its elements by iterating over it. A while loop and
24 eina_array_data_get() can be used, or else one can use the predefined
25 macro EINA_ARRAY_ITER_NEXT(). To free all the elements, a while loop
26 can be used with eina_array_count_get(). Here is an example of use:
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <eina_array.h>
33
34 int main(void)
35 {
36 const char *strings[] = {
37 'first string',
38 'second string',
39 'third string',
40 'fourth string'
41 };
42 Eina_Array *array;
43 char *item;
44 Eina_Array_Iterator iterator;
45 unsigned int i;
46
47 if (!eina_init())
48 {
49 printf ('Error during the initialization of eina0);
50 return EXIT_FAILURE;
51 }
52
53 array = eina_array_new(16);
54 if (!array)
55 goto shutdown;
56
57 for (i = 0; i < 4; i++)
58 {
59 eina_array_push(array, strdup(strings[i]));
60 }
61
62 printf('array count: %d0, eina_array_count_get(array));
63 EINA_ARRAY_ITER_NEXT(array, i, item, iterator)
64 {
65 printf('item #%d: %s0, i, item);
66 }
67
68 while (eina_array_count_get(array))
69 {
70 void *data;
71
72 data = eina_array_pop(array);
73 free(data);
74 }
75
76 eina_array_free(array);
77 eina_shutdown();
78
79 return EXIT_SUCCESS;
80
81 shutdown:
82 eina_shutdown();
83
84 return EXIT_FAILURE;
85 }
86
87 To be continued
88
89
90
91Eina 2 Jul 2010 tutorial_array_page(3)