Union

This is a discussion on Union within the C Programming forums, part of the General Programming Boards category; What's Union And What's The Difference Between Union And Struct?...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    30

    Union

    What's Union And What's The Difference Between Union And Struct?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    A struct consists of one or more variables, a union consists of one variable represented in a number of different ways (a union is always the size of the largest type the variable can be stored as).

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    30
    What are you mean
    "a union is always the size of the largest type the variable can be stored as"?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    A union can hold only one value at a time.....

    i.e.

    union myunion
    {
    int x;
    float y;
    };

    now you can only use x or y at once...

    int main()
    {
    myunion.x = 10;
    printf("%d\n",myunion.x);
    myunion.y=20.4;
    printf("%f\n",myunion.y);
    printf("%d",myunion.x);
    return 0;
    }

    as you can see when you run this the value of myunion.x and myunion.y are stored in the same memory location. When you set myunion.y it overwrites myunion.x . This is a way of space saving really.The union will be the size of its largest member.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    The basic difference as stated above is that:

    A struct can hold many values of many different types.

    A union can hold ONE value of many different types (depending on whatever you wanted).

    struct struct_tag {
    int ival;
    double dval;
    char cval;
    } stvar;

    stvar can hold 3 different values ( 1x INT, 1x DOUBLE, 1x CHAR).

    union union_tag {
    int ival;
    double dval;
    char cval;
    } uvar;

    uvar can hold 1 value of either an integer, double, or character.

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    You won't encounter too many examples in programming where you need to use union. The struct and class types however are used all the time.
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    tamahome

    I agree with Witch King, I have never used Unions in my personal code. I have never really come across a situation when one would help or be needed.

    It's good to know though, just in case.

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Most sutable example is the SDL_Event union.
    The interesting thing is the way they figure out what type
    of event it is.

    Code:
    * Keyboard event structure */
    typedef struct {
    	Uint8 type;	/* SDL_KEYDOWN or SDL_KEYUP */
    	Uint8 which;	/* The keyboard device index */
    	Uint8 state;	/* SDL_PRESSED or SDL_RELEASED */
    	SDL_keysym keysym;
    } SDL_KeyboardEvent;
    
    
    /* General event structure */
    typedef union {
    	Uint8 type;
    	SDL_ActiveEvent active;
    	SDL_KeyboardEvent key;
    	SDL_MouseMotionEvent motion;
    	SDL_MouseButtonEvent button;
    	SDL_JoyAxisEvent jaxis;
    	SDL_JoyBallEvent jball;
    	SDL_JoyHatEvent jhat;
    	SDL_JoyButtonEvent jbutton;
    	SDL_ResizeEvent resize;
    	SDL_ExposeEvent expose;
    	SDL_QuitEvent quit;
    	SDL_UserEvent user;
    	SDL_SysWMEvent syswm;
    } SDL_Event;
    and then switching on the events' type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. "generic" union or something
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 08:55 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 09:12 AM

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