c define arrays in struct with different sizes

前端 未结 3 1213
南方客
南方客 2021-01-21 09:08

I need to define a struct for two types of objects. Both have exactly the same data structure and perform same tasks (member methods).

The ONLY difference is that the a

3条回答
  •  不要未来只要你来
    2021-01-21 09:44

    Even with a union, the struct will be as big as the largest of the two arrays.

    Do one of these:

    1. Ignore the overhead of the biggest array size. (Use one array or a union)
    2. Create a separate struct for each type.
    3. Dynamically allocate the array with malloc.

提交回复
热议问题