Making a dynamic array that accepts any type in C

后端 未结 5 1511
闹比i
闹比i 2021-01-07 18:34

I\'m trying to find a way to make a struct to hold a dynamic array that can work with any data type (Including user defined data types), so far this is what I came up with.<

5条回答
  •  我在风中等你
    2021-01-07 19:09

    Vector(DATATYPE) struct { DATATYPE* data; size_t size; size_t used; } also fails for pointers to functions.

    void* is sufficient and well defined for a pointer to any object, but not so for a pointer to a function.

    C does allow a pointer to a function of one type to be saved as a pointer to a function of another type. By using a union of the two below, code has enough space for saving the pointer to any type. The management of what type and what member used remains open.

    union u_ptr {
      void *object;
      void (*function)();
    }
    

提交回复
热议问题