#include #include #include struct generic_array; struct item_handlers { void (*printer)(void*); void (*item_changer)(void*); void (*set_item)(struct generic_array*, void*, size_t); }; struct generic_array { struct item_handlers* handlers; void* first_item; size_t item_size; size_t item_count; }; void print_all(struct generic_array *ga){ for(int i = 0; i < ga->item_count; i++){ ga->handlers->printer(ga->first_item + ga->item_size * i); } } void double_printer(void* item){ printf("%lf\n", *(double*)item); } void double_changer(void *item){ *(double*)item /= 2; } void generic_set_item(struct generic_array* ga, void* new_item, size_t position){ memcpy(ga->first_item + ga->item_size * position, new_item, ga->item_size); } struct item_handlers double_handlers = {double_printer, double_changer, generic_set_item}; int main(){ struct generic_array array_of_doubles = {&double_handlers, malloc(sizeof(double) * 10), sizeof(double), 10}; *(double*)(array_of_doubles.first_item) = 5.628; *(double*)(array_of_doubles.first_item + array_of_doubles.item_size) = 543.1; *(((double*)(array_of_doubles.first_item)) + 2) = -30.2; double new_item = 16.743; array_of_doubles.handlers->set_item(&array_of_doubles, &new_item, 3); print_all(&array_of_doubles); }