I am new to C but am trying to wrap my head around trying to store arbitrary objects in an array. Structs, integers, chars, functions, etc. Basically something perhaps using void pointers along the lines of (pseudocode):
void *array[] = malloc(10000);
struct MyStruct m = malloc(sizeof(m));
int x = 10;
char c[] = "Look Here";
array[0] = &m;
array[1] = &x;
array[2] = &c;
Essentially I want to have a global array store arbitrary objects sort of like a database, and then fetch them by index somehow.
void *global_array[];
void
get_from_array(int index, void *ptr) {
*ptr = global_array[index];
}
int
main() {
global_array = malloc(10000);
struct MyStruct m = malloc(sizeof(m));
int x = 10;
char c[] = "Look Here";
global_array[0] = &m;
global_array[1] = &x;
global_array[2] = &c;
struct MyStruct m2;
get_from_array(0, &m2);
assert(m == m2);
}
Is anything like this possible?
void*elements of yourglobal_array.