Thread: Question about unions?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Question about unions?

    What makes them any different from structures?

    And how could they possibly be of use?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Union are different from structures only in memory layout....

    I think code spinet can speak more then thousands of words look below



    Code:
    #include <stdio.h>
    
    struct structA {
      int a;
      int b;
      int c;
    }struct_A;
    
    union unionA{
      int a;
      int b;
      int c;
    }union_A;
    
    int main() {
      struct_A.a = 10;
      struct_A.b = 20;
      struct_A.c = 30;
    
      printf("struct_A.a == %d\n", struct_A.a);
      printf("struct_A.b == %d\n", struct_A.b);
      printf("struct_A.c == %d\n", struct_A.c);
    
      union_A.a = 10;
      printf("union_A.a == %d\n", union_A.a);
      printf("union_A.b == %d\n", union_A.b);
      printf("union_A.c == %d\n", union_A.c);
    
      union_A.b = 20;
      printf("union_A.a == %d\n", union_A.a);
      printf("union_A.b == %d\n", union_A.b);
      printf("union_A.c == %d\n", union_A.c);
    
      union_A.c = 30;
      printf("union_A.a == %d\n", union_A.a);
      printf("union_A.b == %d\n", union_A.b);
      printf("union_A.c == %d\n", union_A.c);
    
      return 0;
    }
    output

    struct_A.a == 10
    struct_A.b == 20
    struct_A.c == 30
    union_A.a == 10
    union_A.b == 10
    union_A.c == 10
    union_A.a == 20
    union_A.b == 20
    union_A.c == 20
    union_A.a == 30
    union_A.b == 30
    union_A.c == 30


    i think u will understand

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    • They can save memory, probably not important to you personally unless you're writing embedded stuff.
    • Unions allow you to access the same data in different ways, easier than using bitwise operations and doing it the long way. I'm sure glad Win32's ULARGE_INTEGER is a union, or I'd be hating life right now.
    • If used carefully they can confuse the hell out of anyone who wants to modify your code. Job security.
    • And speaking of jobs, they make for interesting interview questions. If you're into that sort of thing.
    • They're just fun!


    I'll paste the union I referred to above just to demonstrate how unions can make things easier:

    Code:
    typedef union _ULARGE_INTEGER {  
        struct {
            DWORD LowPart; 
            DWORD HighPart; 
        };
        DWORDLONG QuadPart;
    } ULARGE_INTEGER;

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    See the memory offset in union is same for every one but this is not in the case of structures where. union share the same memory for every element

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Thanks, I think with a lil' messing around with it testing concepts I understand my originial questions.
    Unions are extremely similar to structures except they allow one set of storage for all members in union
    and allow only one member at an given time to be used to store a value
    basically it allows for the choice of multiple different types of data, and is much smaller than huge structure


    Only one way is valid at any given time

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    Quote Originally Posted by hugo2x View Post
    Only one way is valid at any given time
    I dislike it when tutorials say this. Yeah I know what they're trying to convey, but it can lead a person to think that they can't set one member and then immediately read a different member of the union or you'll get garbage. This is not the case.

    I use unions 100x more frequently for splitting apart packed data then I do for this "one way at a time" stuff.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    See hugo2x,
    "allow only one member at an given time to be used to store a value
    basically it allows for the choice of multiple different types of data, and is much smaller than huge structure"

    i m not agree with u with this statement

    see union are there for memory distribution among the members
    lets say

    Code:
    union unionE {
      char c;
      int    i;
    }obj;
    if u say obj.i = 10;
    and u want to fill something in c as well which is relevant it will be filled automatically by the above line of statement

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by RockyMarrone View Post
    See hugo2x,
    "allow only one member at an given time to be used to store a value
    basically it allows for the choice of multiple different types of data, and is much smaller than huge structure"

    i m not agree with u with this statement
    It doesn't matter if you agree with it or not. It's correct. You do not...
    Quote Originally Posted by RockyMarrone View Post
    f u say obj.i = 10;
    and u want to fill something in c as well which is relevant it will be filled automatically by the above line of statement
    ...automatically fill out c as well. You can not store two independant values. You pick one member and use it. If you happen to use another member with the data in it, that's fine. But doing so potentially alters the data in all other members. You cannot store two different values at the same time.

    Unions are basically a single memory map, the size of the largest member, and you can access portions of that map by the various handles provided by member names. You cannot store multiple independent values as you would in a structure.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM