QUESTION ANSWER TO THE END OF THE PAGE. FULLY WORKING CODE.
Hi, I would like to do in C what I asked in the title, however, I do not know how to do this. I did this in C ++ thanks to the templates, but à la C. Here is the fully functional C ++ code: List.h (simple database)
* I wonder if with void pointers I can emulate code. The problem is that I saw a link that void * should be avoided because it can cause more problems than it can solve.
Basically, it is a “smart array” that stores pointers to the variables themselves. If I know the size of each pointer and the size of each specified structure, should simple mallocs and reallocs do the right thing?
typedef struct
{
void **list;
int last_item_index;
size_t element_size;
int elements;
int total_size;
int tweak_request_size;
} List;
List_addCopy(List *db, void *ptr_to_new_element)
{
...
db->list[db->last_item_index+1] = malloc(element_size);
...
}
or
List_assign(List *db, void *ptr_to_new_element)
{
db->List = realloc(db->List, element_size*(elements+tweak_request_size));
db->List[db->last_item_index+1] = ptr_to_new_element;
}
List db;
struct funky *now = (funky*)malloc(sizeof(funky));
funky->soul = JamesBrown;
List_addCopy(db, funky);
if (list[0]->soul == JamesBrown)
puts("We did It! :D");
, , - **.
List_add? , / ?
List_assign? .
: p