Thread: Union and struct difference??

  1. #1

    Union and struct difference??

    The topic title says it all. I dont really know the difference, can anyone tell me?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    in C++ the difference is somewhat blurred because supposedly you can inherit from a union . Forget that though.

    union really represents one piece of memory that can be referenced many different ways.

    a struct represents a group of many pieces of data sequentially in memory.

    get it? a union isn't a group as much as it is a reuse of one piece of memory.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Oct 2002
    Union really isn't needed much anymore since todays computers can spare the memory you will waste when having an int and an double even if you only need the double in one situation.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  4. #4

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    The only place I have found these usefull has been with certain Btrieve (yuck!) related database functions. One function call (Get Next Extended) has you initializing a buffer with information such as what criteria you want to have match for retrieval of records and what particular fields you want to extract from the database where that criteria matches. You then pass a pointer to the buffer to the function and it searches through the database and returns the data in the same buffer you gave to the function. When this happens, what is in the buffer after the call is different from what you put into the function prior to the call. Using a union here makes setting up the buffer and then extracting the data from the buffer once the call is complete easy. The union in this case contains two struct elements; one holds the information necessary for defining the criteria and fields you want from the database, the other struct holds those fields from the records you requested. Aside from that, I have never found them particularly usefull.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with using a union into a struct :)
    By g_p in forum C Programming
    Replies: 11
    Last Post: 05-12-2007, 03:21 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Union in a struct?
    By Hunter2 in forum C Programming
    Replies: 2
    Last Post: 06-13-2003, 11:45 AM
  5. Class + Union + Struct = Disaster??
    By Inquirer in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 03:55 PM