Reputation: 901
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?
Upvotes: 5
Views: 7231
Reputation: 1429
I would just declare the function to take both argument types
prepare_tagged_attribute(struct complex_attribute *complex,struct generic_attribute *generic)
Process the first non-NULL argument.
Upvotes: 1
Reputation: 12263
As you declared the function to take a union argument, you do have to actually pass a union argument, not a pointer.
Sidenote: If using gcc, have a look at the transparent_union
attribute. That might be interesting for you, as it actually allows the usage above. Note, howeer, you still have to signal which type of argument you are passing (as much as for the standard way.
A possibly better way would be to bundle type and value information in a single struct like:
struct variable_argument {
enum {
COMPLEX_ARG, GENERIC_ARG
} type;
union union_attribute{
struct complex_attribute *complex;
struct generic_attribute *generic;
};
};
and pass a pointer to such a struct to the function. Note I uses an anonymous struct field for the union (since C99). This way you can refer the fields like:
struct variable_argument s;
... s.complex = ...
Never forget to set s.type
appropriately if you change the type of the union, however.
This struct actually relaces your pointers to one of the other structures. However, you might have them directly in the union instead of using pointers. That simplifies memory allocation/freeing.
Upvotes: 2
Reputation: 1549
You can't (portably) cast a complex_attribute*
to a union_attribute*
. But, if you're using C99, you can use what's called a "compound literal" to effectively create an unnamed union, initialize a member in it, take its address, and pass it to your function, all in line. None of the other answers seem to have mentioned this - here's a working program demonstrating that approach:
#include <stdio.h>
struct generic_attribute{
int current_value;
};
struct complex_attribute{
struct generic_attribute *sub_attributes[20];
};
union union_attribute{
struct complex_attribute *complex;
struct generic_attribute *generic;
};
struct tagged_attribute* prepare_tagged_attribute(int code,
union union_attribute *attribute)
{
if (code == 1) {
printf("in func, generic_attribute is %p\n", attribute->generic);
} else {
printf("in func, complex_attribute is %p\n", attribute->complex);
}
return NULL;
}
int main()
{
struct generic_attribute g;
printf("in main, generic_attribute is %p\n", &g);
prepare_tagged_attribute(1, &(union union_attribute){.generic=&g});
struct complex_attribute c;
printf("in main, complex_attribute is %p\n", &c);
prepare_tagged_attribute(2, &(union union_attribute){.complex=&c});
return 0;
}
See the section on Compound Literals in the standard for more information about this technique. It's been available since C99, but it isn't available in C++, and many C programmers seem not to know about it.
Upvotes: 1
Reputation: 3206
If you're using GCC, and willing to use its language extensions, you can accomplish what you're trying to do with a transparent_union
.
From https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Type-Attributes.html:
Transparent unions are designed for library functions that have multiple interfaces for compatibility reasons. For example, suppose the wait function must accept either a value of type int * to comply with Posix, or a value of type union wait * to comply with the 4.1BSD interface. If wait's parameter were void *, wait would accept both kinds of arguments, but it would also accept any other pointer type and this would make argument type checking less useful. Instead, might define the interface as follows:
typedef union
{
int *__ip;
union wait *__up;
} wait_status_ptr_t __attribute__ ((__transparent_union__));
pid_t wait (wait_status_ptr_t);
In your example, you'd declare union_attribute
like this:
typedef union {
struct complex_attribute *complex;
struct generic_attribute *generic;
} union_attribute __attribute__ ((__transparent_union__));
And then you can call prepare_tagged_attribute
exactly how you proposed.
struct tagged_attribute* prepare_tagged_attribute(int code, union_attribute attribute)
{
attribute.generic_attribute->current_value = 1; // e.g.
return NULL; // e.g.
}
prepare_tagged_attribute(code, pointer_to_complex_structure);
Upvotes: 5
Reputation: 5290
Don't use unions at all. Since you also pass a code indicating what kind of pointer is being used, use a pointer to void and just pass the structure pointer:
prepare_tagged_attribute(2, pointer_to_complex_structure);
Then dynamically cast your pointer based on the code:
if( code == 2 ){
struct complex_attribute* p = attribute ;
}
Upvotes: 1
Reputation: 372784
Since there is no polymorphism or overloading in C, I'm not sure there's a perfect solution to this.
Your solution will work, provided that you wrap up your pointer in a union_attribute
:
union union_attribute argument;
argument.complex = pointer_to_complex_structure;
prepare_tagged_attribute(2, &argument);
It's a bit annoying that you have to do this wrap, so one option would be to introduce a new function to do it for you:
union union_attribute wrap_complex(struct complex_attribute* attr) {
union union_attribute result;
result.complex = attr;
return result;
}
Then, rewrite your prepare_tagged_attribute
function to take in a union
by value rather than by pointer:
struct tagged_attribute* prepare_tagged_attribute(int code, union union_attribute attribute)
Then, you can say something like
prepare_tagged_attribute(2, wrap_complex(pointer_to_complex_structure));
It's not perfect, for sure, but it works and type-checks.
Hope this helps!
Upvotes: 3