GLib Reference Manual | |||
---|---|---|---|
<<< Previous Page | Home | Up | Next Page >>> |
#include <glib.h> struct GArray; GArray* g_array_new (gboolean zero_terminated, gboolean clear, guint element_size); #define g_array_append_val (a,v) GArray* g_array_append_vals (GArray *array, gconstpointer data, guint len); #define g_array_prepend_val (a,v) GArray* g_array_prepend_vals (GArray *array, gconstpointer data, guint len); #define g_array_insert_val (a,i,v) GArray* g_array_insert_vals (GArray *array, guint index, gconstpointer data, guint len); GArray* g_array_remove_index (GArray *array, guint index); GArray* g_array_remove_index_fast (GArray *array, guint index); #define g_array_index (a,t,i) GArray* g_array_set_size (GArray *array, guint length); void g_array_free (GArray *array, gboolean free_segment); |
Arrays are similar to standard C arrays, except that they grow automatically as elements are added.
Array elements can be of any size (though all elements of one array are the same size), and the array can be automatically cleared to '0's and zero-terminated.
To create a new array use g_array_new().
To add elements to an array, use g_array_append_val(), g_array_append_vals(), g_array_prepend_val(), and g_array_prepend_vals().
To access an element of an array, use g_array_index().
To set the size of an array, use g_array_set_size().
To free an array, use g_array_free().
Example 1. Using a GArray to store gint values.
GArray *garray; gint i; /* We create a new array to store gint values. We don't want it zero-terminated or cleared to 0's. */ garray = g_array_new (FALSE, FALSE, sizeof (gint)); for (i = 0; i < 10000; i++) g_array_append_val (garray, i); for (i = 0; i < 10000; i++) if (g_array_index (garray, gint, i) != i) g_print ("ERROR: got d instead of d\n", g_array_index (garray, gint, i), i); g_array_free (garray, TRUE); |
GArray* g_array_new (gboolean zero_terminated, gboolean clear, guint element_size); |
Creates a new GArray.
#define g_array_append_val(a,v) |
Adds the value on to the end of the array. The array will grow in size automatically if necessary.
Note: g_array_append_val() is a macro which uses a reference to the value parameter v. This means that you cannot use it with literal values such as "27". You must use variables.
GArray* g_array_append_vals (GArray *array, gconstpointer data, guint len); |
Adds len elements onto the end of the array.
#define g_array_prepend_val(a,v) |
Adds the value on to the start of the array. The array will grow in size automatically if necessary.
This operation is slower than g_array_append_val() since the existing elements in the array have to be moved to make space for the new element.
Note: g_array_prepend_val() is a macro which uses a reference to the value parameter v. This means that you cannot use it with literal values such as "27". You must use variables.
GArray* g_array_prepend_vals (GArray *array, gconstpointer data, guint len); |
Adds len elements onto the start of the array.
This operation is slower than g_array_append_vals() since the existing elements in the array have to be moved to make space for the new elements.
#define g_array_insert_val(a,i,v) |
Inserts an element into an array at the given index.
Note: g_array_insert_val() is a macro which uses a reference to the value parameter v. This means that you cannot use it with literal values such as "27". You must use variables.
GArray* g_array_insert_vals (GArray *array, guint index, gconstpointer data, guint len); |
Inserts len elements into a GArray at the given index.
GArray* g_array_remove_index (GArray *array, guint index); |
Removes the element at the given index from a GArray. The following elements are moved down one place.
GArray* g_array_remove_index_fast (GArray *array, guint index); |
Removes the element at the given index from a GArray. The last element in the array is used to fill in the space, so this function does not preserve the order of the GArray. But it is faster than g_array_remove_index().
array : | a GArray. |
index : | the index of the element to remove. |
Returns : | the GArray. |
#define g_array_index(a,t,i) |
Returns the element of a GArray at the given index. The return value is cast to the given type.
Example 2. Getting a pointer to an element in a GArray.
EDayViewEvent *event; /* This gets a pointer to the 3rd element in the array of EDayViewEvent structs. */ event = &g_array_index (events, EDayViewEvent, 3); |
GArray* g_array_set_size (GArray *array, guint length); |
Sets the size of the array, expanding it if necessary. If the array was created with clear set to TRUE, the new elements are set to 0.