Structures & Unions

This is a discussion on Structures & Unions within the C Programming forums, part of the General Programming Boards category; What is the difference between a structure and Union...

  1. #1
    Unregistered
    Guest

    Structures & Unions

    What is the difference between a structure and Union

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    23
    well, the books will tell you that they are very similar i.e. how to declare them, how to access the variables even declaring them

    e.g.

    struct example{
    char var1;
    char var2;
    };

    union next_eg{
    char var3;
    char var4;
    };

    struct example test;
    union next_eg test2;

    test.var1 = 'a';
    test.var2 = 'b';

    test2.var3 = 'c';
    test2.var4 = 'd';

    however, struct test has the size of 2 char whereas union test2 has the size of 1 char only.

    this is because a union will take the size of the largest variable.

    hence, if there were a int declared in the union then the size of that union will be 2 bytes, since int is 2 bytes long and char is 1 byte long.

    refer to above example.



    this also means that for a struct, you can use the variables at any time. for the union you can only use 1 of them at any one time.

    clear?

    it appears that the benefit of using unions is that if you only use 1 variable at any time, then you will save space.

    feedback is welcome.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    >>however, struct test has the size of 2 char whereas union test2 has the size of 1 char only.

    Not totally true as the struct or union may be padded or 'byte aligned'. You can never assume sizeof(STRUCT)==sum(sizeof(elements of STRUCT)).

    >>hence, if there were a int declared in the union then the size of that union will be 2 bytes, since int is 2 bytes long and char is 1 byte long.

    Again not totally true on all OS and systems. int's being 4 bytes, UNICODE strings 2bytes ect.


    Uses of unions are limited in my experience.

    One good use is to return from a function.
    Code:
    typedef union
    {
         int          iErrorCode;
         RETURN_BMP   BMPResultValues;// a structure
         RETURN_JPG   JPGResultValues;// a structure
         RETURN_TIFF  TIFFResultValues;// a structure
    } UNION_EXAMPLE;
    
    UNION_EXAMPLE  My_Function(int iData, ect);
    If the function generates an error it can return ONLY the error in int form (iErrorCode) or if it works it can return ONE whole filled structure.
    It allows a function to act differently depending on the data sent to it.

    The variable filled remains the same allowing some more 'reusablility' (if that is a word).
    Last edited by novacain; 06-20-2002 at 04:27 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-21-2008, 05:18 PM
  2. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  3. Structures - Unions
    By AProg in forum C Programming
    Replies: 16
    Last Post: 05-27-2003, 01:43 AM
  4. Structures and unions
    By Paninaro in forum C Programming
    Replies: 6
    Last Post: 06-21-2002, 02:35 PM
  5. Replies: 7
    Last Post: 12-29-2001, 11:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21