Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Union as an argument to a function in C

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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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);
over 4 years ago · Santiago Trujillo Report

0

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!

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!