what is the use of unions in c?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shivapadma
    New Member
    • Mar 2007
    • 40

    #1

    what is the use of unions in c?

    At any point, we can use only one variable declared in union.

    then why to use unoins in programs.

    example
    union student
    {
    char name [10];
    int rollno;
    };

    At any point, we can use either "name" variable or "rollno" variable devcalred in union student.

    please,kindly reply to this question.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    In a union, every member is allocated the same piece of storage (that's why a union is as big as its largets member).
    Proper use of it can save memory .

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Unions go back as far as Fortran IV with its overlays of COMMON blocks; they're fun.

      kind regards,

      Jos

      Comment

      • jorba101
        New Member
        • Jun 2008
        • 86

        #4
        A typical case that comes to my mind in which a union can be useful is following:

        -Usually, a field in a struct that only has sense under certain conditions, normally also specified in another field of the struct. For instance:

        typedef struct
        {
        char typeOfPacket; // 0: packet in format A; 1: packet in format B
        union
        {
        format_a_t header_a;
        format_b_t header_b;
        }
        char payload[ PACKET_LEN ]
        } typeOfPacket;

        This way you can store two types of packet headers, with the memory space for a single header (i.e., assuming the packet can be of type 'a' or 'b', but not both at same time). Typically there will be a field in the struct telling which one the values in the union should be used (in this case, typeOfPacket). But it's up to your code using it one way or another.

        I've also used unions to access data in different ways:

        typedef union
        {
        float value;
        char bytesInValue[ 4 ];
        } floatingNum_t;

        This way I can store a 32-bit float value in floatingNum.val ue (assuming my float is 32bit), and afterwards take one by one the bytes in the memory (by acessing floating.value[0...3]).

        Though, when using unions it is very important to be aware on how your platform addresses information, and also data endianness. Specially care when placing structs inside unions: Lots of architectures, like ARM, leave unused memory positions in memory, according to some internal rules, which you need to know. This can be critical when playing with unions.

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          Some unions use a "type" variable to tell the client what data is being passed. For example, X windows uses a union for all of its events. The first variable defined is an a event_type that is used to tell what data follows.

          The client then uses a if-else conditional on the type value to figure out what event happened and what data to expect.

          Comment

          Working...