How to check what type is currently used in union?

14,699

Solution 1

Is there any better solution?

No, the solution that you showed is the best (and the only) one. unions are pretty simplistic - they do not "track" what you've assigned to what. All they do is let you reuse the same memory range for all their members. They do not provide anything else beyond that, so enclosing them in a struct and using a "type" field for tracking is precisely the correct thing to do.

Solution 2

C does not automatically keep track of which field in a union is currently in use. (In fact, I believe reading from the "wrong" field results in implementation defined behavior.) As such, it is up to your code to keep track of which one is currently used / filled out.

Your approach to keeping a separate 'uniontype' variable is a very common approach to this, and should work well.

Solution 3

There is no way to directly query the type currently stored in a union.

The only ways to know the type stored in a union are to have an explicit flag (as in your mystruct example), or to ensure that control only flows to certain parts of the code when the union has a known active element.

Solution 4

Depending on the application, if it is a short lived object you may be able to encode the type in the control flow, ie. have separate blocks/functions for both cases

  struct value {
      const char *name;
      myunion u;
  };

  void throwBall(Ball* ball)
  {
     ...
     struct value v;
     v.name = "Ball"; v.u.b = 1.2;
     process_value_double(&v);      //double
     struct value v2;
     v2.name = "Age";
     v2.u.a = 19;
     check_if_can_drive(&v2);       //int
     ...
  }

  void countOranges()
  {
       struct value v;
       v.name = "counter";
       v.u.a = ORANGE;
       count_objects(&v);          //int
  }

Solution 5

Warning: the following is just for learning purpose:

You could use some ugly tricks to do so (as long as the data types in your union have different sizes, which is the present case):

#include <stdio.h>

typedef union someunion {
  int a;
  double b;
} myunion;

typedef struct somestruct {
  int uniontype;
  myunion numbers;
} mystruct;


#define UPDATE_CONTENT(container, value) if ( \
                                             ((sizeof(value) == sizeof(double)) \
                                              ? (container.uniontype = ((container.numbers.b = value), 2)) \
                                              : (container.uniontype = ((container.numbers.a = value), 1))))

int main()
{
  mystruct my_container;

  UPDATE_CONTENT(my_container, 42);
  printf("%d\n", my_container.uniontype);
  UPDATE_CONTENT(my_container, 37.1);
  printf("%d\n", my_container.uniontype);
  return (0);
}

But I advise you never do this.

Share:
14,699

Related videos on Youtube

tomdavies
Author by

tomdavies

Updated on October 09, 2022

Comments

  • tomdavies
    tomdavies over 1 year

    let's say we have a union:

    typedef union someunion {
        int a;
        double b;
    } myunion;
    

    Is it possible to check what type is in union after I set e.g. a=123? My approach is to add this union to some structure and set uniontype to 1 when it's int and 2 when it's double.

    typedef struct somestruct {
        int uniontype
        myunion numbers;
    } mystruct;
    

    Is there any better solution?

  • Jim Balter
    Jim Balter about 11 years
    A better solution is to use enum values rather than 1, 2, etc.
  • Jerry Yang
    Jerry Yang almost 6 years
    And actually, this technique is adopted very widely in kernel network programming, for the content of a received packet(datagram) is usually unknown before checking type code in header.
  • onlycparra
    onlycparra about 3 years
    C: my blessing is my curse
  • Jeppe
    Jeppe over 2 years
    What would happen if the number happened to be 83 (the ascii decimal for S)? And you're relying on knowing all the values it can hold in advance.