Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
If you don't mind the extra memory, you can do it with an extra array: struct basket { char *item_memory; char **items; int itemcount; }; /* I omitted any error handling */ voi...
Answer
#1: Initial revision
If you don't mind the extra memory, you can do it with an extra array: ```c struct basket { char *item_memory; char **items; int itemcount; }; /* I omitted any error handling */ void initialize(basket *b, int itemlen, int itemcount) { int item_index; b->item_memory = malloc(itemlen * itemcount); b->items = malloc(sizeof(char*) * itemcount); b->itemcount = itemcount; for (item_index = 0; item_index < itemcount; ++item_index) b->items[item_index] = item_memory + itemlen ** item_index; } ``` Of course here the actual storage is not in `*items` but in `*item_memory`. You cannot avoid that if you want to use double index on `items`; the only thing in C you can apply the index operator to are pointers, therefore `*index` must be a pointer.