Sun, 18 Feb 2024 13:16:38 +0100
make cx_array_simple_add() automatically take the address of the element
src/cx/array_list.h | file | annotate | diff | comparison | revisions | |
tests/test_list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/array_list.h Sun Feb 18 13:01:09 2024 +0100 1.2 +++ b/src/cx/array_list.h Sun Feb 18 13:16:38 2024 +0100 1.3 @@ -61,6 +61,18 @@ 1.4 size_t name##_capacity; 1.5 1.6 /** 1.7 + * Initializes an array declared with cx_array_declare(). 1.8 + * 1.9 + * The memory for the array is allocated with stdlib malloc(). 1.10 + * @param array the array 1.11 + * @param capacity the initial capacity 1.12 + */ 1.13 +#define cx_array_initialize(array, capacity) \ 1.14 + array##_capacity = capacity; \ 1.15 + array##_size = 0; \ 1.16 + array = malloc(sizeof(array[0]) * capacity); 1.17 + 1.18 +/** 1.19 * Defines a reallocation mechanism for arrays. 1.20 */ 1.21 struct cx_array_reallocator_s { 1.22 @@ -181,7 +193,7 @@ 1.23 * @param size a pointer to the size of the target array 1.24 * @param capacity a pointer to the target array's capacity - must not be \c NULL 1.25 * @param elem_size the size of one element 1.26 - * @param elem the element to add 1.27 + * @param elem a pointer to the element to add 1.28 * @param reallocator the array reallocator to use, or \c NULL if reallocation shall not happen 1.29 * @return zero on success, non-zero error code on failure 1.30 */ 1.31 @@ -192,10 +204,10 @@ 1.32 * Convenience macro that uses cx_array_add() with a default layout and the default reallocator. 1.33 * 1.34 * @param array the name of the array (NOT a pointer to the array) 1.35 - * @param elem the element to add 1.36 + * @param elem the element to add (NOT a pointer, address is automatically taken) 1.37 */ 1.38 #define cx_array_simple_add(array, elem) \ 1.39 - cx_array_simple_copy(array, array##_size, elem, 1) 1.40 + cx_array_simple_copy(array, array##_size, &(elem), 1) 1.41 1.42 /** 1.43 * Swaps two array elements.
2.1 --- a/tests/test_list.c Sun Feb 18 13:01:09 2024 +0100 2.2 +++ b/tests/test_list.c Sun Feb 18 13:16:38 2024 +0100 2.3 @@ -74,7 +74,7 @@ 2.4 CX_TEST_ASSERT(stackarray_size == 5); 2.5 CX_TEST_ASSERT(stackarray_capacity == 5); 2.6 2.7 - result = cx_array_simple_add(heaparray, &elem); 2.8 + result = cx_array_simple_add(heaparray, elem); 2.9 CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); 2.10 CX_TEST_ASSERT(heaparray[0] == 2); 2.11 CX_TEST_ASSERT(heaparray[1] == 3); 2.12 @@ -85,7 +85,7 @@ 2.13 CX_TEST_ASSERT(heaparray_capacity == 5); 2.14 2.15 heaparray_size = 5; 2.16 - result = cx_array_simple_add(heaparray, &elem2); 2.17 + result = cx_array_simple_add(heaparray, elem2); 2.18 CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); 2.19 CX_TEST_ASSERT(heaparray[0] == 2); 2.20 CX_TEST_ASSERT(heaparray[1] == 3);