I am wondering if it is possible to use unions as arguments to a function:
Let's say I have two structures:
struct complex_attribute{
struct generic_attribute *sub_attributes[20];
};
struct generic_attribute{
int current_value;
};
And a union of these two:
union union_attribute{
struct complex_attribute *complex;
struct generic_attribute *generic;
};
I want to create a function that takes in either a complex_attribute or a generic_attribute:
struct tagged_attribute* prepare_tagged_attribute(int code, union union_attribute *attribute)
However, when I make a call to this function
prepare_tagged_attribute(2, pointer_to_complex_structure);
I get this error:
passing argument 2 of ‘prepare_tagged_attribute’ from incompatible pointer type
So I take it that pointer to a a complex structure is not necessarily a pointer of type union (which makes sense)... but then is it possible to use unions in this way?
void*
, which means that you could pass anint*
, achar**
, etc. even if you're not supposed to. I think the OP's idea is to make it hard to get this wrong. – Stonebroke