所以我有一个包含一个联合结构如下:的指针访问指针工会
struct FILL{
char *name;
int id;
};
struct TEST{
union{
struct FILL *fill;
int type;
} *uni;
};
我不明白如何在结构中访问工会成员。我一直试图做到这一点如下:
struct TEST *test_struct, *test_int;
test_struct = malloc(sizeof(struct TEST));
test_struct->uni = malloc(sizeof(struct TEST));
test_struct->uni->fill->name = NULL;
test->struct->uni->fill->id = 5;
test_int = malloc(sizeof(int));
test_int->uni->type = 10;
但我得到segfaults,当我尝试这个。我访问这些错误吗?我应该怎么做呢?
编辑:对不起,我是专注于格式化和我搞砸了测试声明。它已被修复。
如何提供'结构TEST'的完整,准确的申报?你提交的那个似乎被截断了。 –
没有遗憾,是我犯的一个错误,我已经纠正了,但,这是完整的声明。 – Phenom588
什么是'type'联盟内部的目的,如果你永远不会成为能够使用它?你应该把'type'放在struct中,其余的放在union中。 – 2501