【发布时间】:2015-06-26 18:27:53
【问题描述】:
我想知道是否可以使用联合作为函数的参数:
假设我有两个结构:
struct complex_attribute{
struct generic_attribute *sub_attributes[20];
};
struct generic_attribute{
int current_value;
};
这两者的结合:
union union_attribute{
struct complex_attribute *complex;
struct generic_attribute *generic;
};
我想创建一个接受 complex_attribute 或 generic_attribute 的函数:
struct tagged_attribute* prepare_tagged_attribute(int code, union union_attribute *attribute)
但是,当我调用这个函数时
prepare_tagged_attribute(2, pointer_to_complex_structure);
我收到此错误:
passing argument 2 of ‘prepare_tagged_attribute’ from incompatible pointer type
所以我认为指向复杂结构的指针不一定是联合类型的指针(这是有道理的)......但是可以以这种方式使用联合吗?
【问题讨论】:
标签: c function structure unions