Hi! I'm new to C (about 5 months) and really wanted to have generic typesafe array operations. I found out about macros and tried to implement them in that way. Was there a better way to do this? here is the repo (https://github.com/Yoguti/typesafe-generic-arrays)
but the main part is essentially:
#ifndef GEN_ARR
#define GEN_ARR
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DEFINE_ARRAY_TYPE(T) \
typedef struct { \
T *data; \
size_t length; \
size_t capacity; \
} Array_##T; \
\
static void array_init_##T(Array_##T *arr, size_t capacity) { \
arr->data = (T *)malloc(sizeof(T) * capacity); \
arr->length = 0; \
arr->capacity = capacity; \
} \
static void array_reverse_##T(Array_##T *arr) { \
for (int i = 0; i < (int)arr->length / 2; i++) { \
T temp = arr->data[i]; \
arr->data[i] = arr->data[arr->length - 1 - i]; \
arr->data[arr->length - 1 - i] = temp; \
} \
} \```