Dynamic array implementation I would like to use code below for general purpose dynamic array implementation in my future code. How does it look?

array.h:

#include &lt;stdlib.h&gt;

/* This file describes structures and functions
   for general purpose dynamic array.
*/

struct array {
    void *data;       /* Points to where the data is */
    int numElements;  /* Number of elements */
    int sizeElem;     /* Size of a single elements */
    int capacity;     /* Current total space in number of elements */
};

/* macros for accessing array internals */
#define array_index(ARRAY, INDEX) (void *)((unsigned)((ARRAY)-&gt;data)+(INDEX*((ARRAY)-&gt;sizeElem)))
#define array_len(ARRAY) (ARRAY)-&gt;numElements
#define array_cap(ARRAY) (ARRAY)-&gt;capacity

static inline void array_init(struct array *arr, int capacity, int sizeElem) {
    arr-&gt;data = malloc(capacity * sizeElem);
    arr-&gt;capacity = capacity;
    arr-&gt;sizeElem = sizeElem;
    arr-&gt;numElements = 0;
}

/* Add an element to array. Expand array if necessary. */
static inline void array_add(struct array *arr, void *data) {
    if (arr-&gt;capacity &lt;= arr-&gt;numElements) {
        int new_capacity = 2 * arr-&gt;capacity;
        arr-&gt;data = realloc(arr-&gt;data, new_capacity * arr-&gt;sizeElem);
        arr-&gt;capacity = new_capacity;
    }
    memcpy(array_index(arr,arr-&gt;numElements), data, arr-&gt;sizeElem);
    arr-&gt;numElements++;
}

/* Macro for traversing array elements. */
#define array_for_each(TYPE, ELEM, ARRAY) \
    for (TYPE *ELEM=(ARRAY)-&gt;data; (ELEM - (TYPE *)(ARRAY)-&gt;data) &lt; (ARRAY)-&gt;numElements; ELEM++)


/* Removes the element at given index and moves other elements if necessary. */
static inline void array_delete_index(struct array *arr, int index) {
    int movables = arr-&gt;numElements - index - 1;
    memcpy(array_index(arr, index),
           (void *)((unsigned)array_index(arr, index) + arr-&gt;sizeElem),
           movables * arr-&gt;sizeElem);
    arr-&gt;numElements--;
}

/* If array capacity is too much compared to number of elements,
   reduce array capacity
*/
static inline void array_free_capacity(struct array *arr) {
    if (arr-&gt;capacity &gt; 2*arr-&gt;numElements) {
        int new_capacity = arr-&gt;capacity / 2;
        arr-&gt;data = realloc(arr-&gt;data, new_capacity * arr-&gt;sizeElem);
        arr-&gt;capacity = new_capacity;
    }
}

/* Probably useless macro */
#define array_free(ARRAY) free((ARRAY)-&gt;data)

/* Search for an element and return an index if found. Otherwise, return -1
   Comparison function should return 0 if two items should be considered equal.
*/
static inline int array_search(struct array *arr, void *target, int (*cmp)(void *first, void *second)) {
    for(int i=0; i&lt;arr-&gt;numElements; i++) {
        if(cmp(array_index(arr, i), target) == 0) return i;
    }
    return -1;
}

/* Returns a pointer to the element at given index, if it doesn't
    exist, return NULL
*/
static inline void *array_get_index(struct array *arr, int index) {
    if (index &gt; (arr-&gt;numElements - 1)) return NULL;
    return array_index(arr, index);
}


How the code is used:

First you initialize the array with a data type and the initial array capacity, measured in the number of elements that the array can hold.

After that, the functions in array.h can be used to add, remove, traverse the array, and more.

Example:

struct task {
    char *task_name;
    int task_id;
};

struct array my_tasks;
/* Define an dynamic array of struct tasks */
array_init(&amp;my_tasks, 2, sizeof(struct task));
Some good practices:

Assert on NULL parameter

Whenever you have a function passing an array you should either do a runtime check to see if it's NULL or do an assert(array). To catch bugs where a NULL array is passed to the functions.

Check return values for alloc functions

You should always make sure you get a non-NULL result from any allocation, malloc, realloc and such to fail gracefully, or at least exit(1) with a nice out of memory message. Good for catching bugs where accidentally an uninitialized count value is passed to an allocating function.

Check for negative input

Since for instance your numElements is an int instead of size_t you should either change it, or at least check for negative values.

Deleting many values

I don't know how common a use case this would be, but since on each delete, you might move a large chunk of values. Adding a function that either takes a list of indices to delete and do it in one go, by simply refilling the array with those indices excluded. Otherwise iterating over and deleting values can be a huge operation depending on the size of the array.

Destroy function

I agree with this comment:

/* Probably useless macro */
#define array_free(ARRAY) free((ARRAY)-&gt;data)


This should be a function that both resets the numElements = 0, capacity = 0 and does the free(array-&gt;data) as well as array-&gt;data = NULL.

This ensures that any reuse of the array will still result in a reallocation and hence not writing to freed memory. However please note that array_add in this case must check that capacity &gt; 0 and at least set it to 1, otherwise the realloc will be 0.