Storing arbitrary objects in array in C

孤者浪人 提交于 2020-04-18 05:45:24

问题


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:


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;
}



回答2:


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;
}



来源:https://stackoverflow.com/questions/61156025/storing-arbitrary-objects-in-array-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!