我对C很陌生,所以请耐心等待。 我有一个包含可变大小的其他结构的工会这样的结构:动态数组,结构中包含一个联合C
typedef struct _obj_struct {
struct_type type;
union obj {
struct1 s1;
struct2 s2;
struct3 s3;
} s_obj;
} obj_struct;
typedef struct _t_struct {
unsigned int number_of_obj;
obj_struct* objs;
other_struct os;
unsigned int loop;
} t_struct;
的struct_type是我们在联合使用结构类型。 我如何浏览objs中的所有元素? 这是做到这一点的正确方法:
struct1 s1;
struct2 s2;
struct3 s3;
for (j=0; j<t_struct.number_of_obj; j++)
{
switch (t_struct.obj[j].type) {
case STRUCT1:
s1 = t_struct.objs[j].s_obj.s1;
break;
case STRUCT2:
s2 = t_struct.objs[j].s_obj.s2;
break;
}
}
非常困惑:你必须定义'STRUCT1'和'STRUCT2'然后就可以使用 –
你可能会需要有使用'S3 = t_struct.obj为STRUCT3的情况下, [j] .s_obj.s3;',据推测。 –