#define array_idx(array, idx) \
ARRAY_TYPE_CAST_CONST(array)array_idx_i(&(array)->arr, idx)
+/* Using *array_idx() will fail if the compiler doesn't support typeof().
+ The same can be done with array_idx_elem() for arrays that have pointers. */
+#ifdef HAVE_TYPEOF
+# define array_idx_elem(array, idx) \
+ (TRUE ? *array_idx(array, idx) : \
+ COMPILE_ERROR_IF_TRUE(sizeof(**(array)->v) != sizeof(void *)))
+#else
+# define array_idx_elem(array, idx) \
+ (*(void **)array_idx_i(&(array)->arr, idx))
+#endif
static inline void *
array_get_modifiable_i(struct array *array, unsigned int *count_r)
unsigned int a, b, c;
};
+static void test_array_elem(void)
+{
+ ARRAY(struct foo *) foos;
+ struct foo *nfoo;
+ unsigned int i;
+
+ test_begin("array elem");
+ t_array_init(&foos, 32);
+
+ for (i = 1; i <= 3; i++) {
+ nfoo = t_new(struct foo, 1);
+ nfoo->a = i;
+ array_push_back(&foos, &nfoo);
+ }
+
+ struct foo *const *foo_p = array_idx(&foos, 1);
+ unsigned int idx = 1;
+ struct foo *foo = array_idx_elem(&foos, idx++);
+ /* make sure idx isn't expanded multiple times in the macro */
+ test_assert(idx == 2);
+ test_assert(*foo_p == foo);
+
+ i = 1;
+ array_foreach_elem(&foos, foo) {
+ test_assert(foo->a == i);
+ i++;
+ }
+ test_end();
+}
+
static void test_array_count(void)
{
ARRAY(struct foo) foos;
void test_array(void)
{
+ test_array_elem();
test_array_count();
test_array_foreach();
test_array_foreach_elem_string();