0

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?

1
  • You would presumably need a parallel array that records the size of each object pointed to be the void* elements of your global_array. Commented Apr 11, 2020 at 11:36

2 Answers 2

3

Yes. You can create a void double pointer void** And allocate it space of (say 10000) void pointers with malloc. It can be indexed and it effectively acts as an array of void* type

For the code mentioned in your question, it would be something like

# include <stdio.h>
# include <stdlib.h>


void **array;

typedef struct MyStruct{
  int a;
  char b;
}MyStruct;

int main()
{
  array = malloc(sizeof(void*)*10000);
  struct MyStruct* m = (MyStruct*)malloc(sizeof(MyStruct));
  m->a=1;
  m->b='x';
  int x = 10;
  char c[] = "Look Here";
  array[0] = m;
  array[1] = &x;
  array[2] = &c;
  printf("%d %c\n%d\n%s\n",((MyStruct*)(array[0]))->a,((MyStruct*)(array[0]))->b,*(int*)(array[1]),(char*)(array[2]));
  return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@RobertSsupportsMonicaCellio is it fine now?
@RobertSsupportsMonicaCellio Anything else? Really sorry for the edit where it didn't compile
No, as far as I can see everything should be fine. Additional, the OP wants furthermore to use a function get_from_array() to assign the object the provided pointer (in the argument list) point to but the design of it yet is susceptible for major issues if the provided pointer doesn´t match to the type of the object the relative pointer in the array point to. High-probably too much demand on you to correct this as you answered the main question already.
1

If you want to store many types, you need to save the size or the type of each variable. You should use the struct and union. For example:

typedef enum EltType { TYPE_STRING, TYPE_INT, TYPE_FLOAT } TYPE;
typedef struct Element {
  TYPE type;
  union {
    char  *str;
    int    i;
    float  f;
  };
}ELEMENT;

The test:

#include <stdio.h>

typedef enum EltType { TYPE_STRING, TYPE_INT, TYPE_FLOAT } TYPE;
typedef struct Element {
  TYPE type;
  union {
    char  *str;
    int    i;
    float  f;
  };
}ELEMENT;

void print_value(ELEMENT elt) {
    switch (elt.type) {
        case TYPE_STRING:
           printf("%s\n", elt.str);
           break;
        case TYPE_INT:
           printf("%d\n", elt.i);
           break;
        case TYPE_FLOAT:
           printf("%f\n", elt.f);
           break;
    }
}
int main(int argc, char const *argv[])
{
    ELEMENT elt1, elt2, elt3; 
    elt1.type = TYPE_STRING;
    elt1.str = "string";

    elt2.type = TYPE_INT;
    elt2.i = 5;

    elt3.type = TYPE_FLOAT;
    elt3.f = 1.2;

    print_value(elt1);
    print_value(elt2);
    print_value(elt3);

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.