Results 1 to 11 of 11

Thread: [RESOLVED] How Unions will be used practically in C?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Resolved [RESOLVED] How Unions will be used practically in C?

    Hi All,
    I posted this thread once, but i could not get proper reply. if any one know, please resopnd to this thread.

    I came to know that, Unions will be very much useful in Embeded programing. but i don't know how they will be used....if you know this please let me know it.

    Thanks:
    regards:
    raghunadhs.v

  2. #2
    Addicted Member SpS's Avatar
    Join Date
    Jul 2005
    Posts
    201

    Re: How Unions will be used practically in C?

    A union is essentially a structure in which all of the fields overlay each other; you can only use one field at a time. (You can also cheat by writing to one field and reading from another, to inspect a type's bit patterns or interpret them differently, but that's obviously pretty machine-dependent.) The size of a union is the maximum of the sizes of its individual members.

    The primary usefulness of a union is to conserve space, since it provides a way of letting many different types be stored in the same space. Unions also provide crude polymorphism. However, there is no checking of types, so it is up to the programmer to be sure that the proper fields are accessed in different contexts. The relevant field of a union variable is typically determined by the state of other variables, possibly in an enclosing struct.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: How Unions will be used practically in C?

    Thanks SPS,
    it really a good explenation. could you please provide me a sample example, regaring to the practical usage of union.

    Actually I tried, but could not find any proper example.
    Thanks:
    regards:
    raghunadhs.

  4. #4
    Addicted Member SpS's Avatar
    Join Date
    Jul 2005
    Posts
    201

    Re: How Unions will be used practically in C?

    Example
    Code:
    #include <iostream>
    using namespace std;
    
    union Packed
    {
        char i;
        short j;
        int k;
        long l;
        float f;
        double d;
    };
    
    int main()
    {
        cout << "sizeof(Packed) = " << sizeof(Packed) << endl;
        Packed x;
        x.i = 'c';
        cout << x.i << endl;
        x.d = 3.14159;
        cout << x.d << endl;
    }
    #Appreciate others by rating good posts !!

    #The Software Peter Principle is in operation when unwise developers "improve" and "generalize" the software until they themselves can no longer understand it, then the project slowly dies.

    #People who are still ignorant of their ignorance are dangerous.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: How Unions will be used practically in C?

    Hi SPs,
    i know that unions will save memory, and instead of taking few variable declerations(say 5), if we place those 5 variables in a union, then the memory will be allocated, based on one of the fields whose memory is higher than others. it will be useful if we access only one filed at a time(and will be failed if try to access all the fileds).
    The example what you provided is defnetly a good one, and is very much useful for beginners.but i need an example regarding to unions, where it will be very much useful(mandetory)? for example....
    there is a oil tank monitor, the operator can do one operation at a time. the operaitons may be (1) finding temperature (consider it as a floting point)
    (2)volume of oil (consider it as integer..(eventhough some times float))
    the operator can press (1) or (2), if he presses (1)
    union TankMoniter
    {
    float Temperature;
    int Volume;
    }T;

    T.temperature=readtemperature();
    ....
    ....
    So,here memory will be saved....I know that this is not a proper one, if u say like this one, it will be more useful to me.

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How Unions will be used practically in C?

    There is no real situation where unions are mandatory these days. As I said in your other thread, the only real use for unions these days is to save space on architectures like the PS2/3, Xbox, etc.

    It should also be noted that, only 1 member of a union can be assigned at any given time, and that is the only member that can be accessed.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: How Unions will be used practically in C?

    A nifty trick with unions is to create a bit field where each bit can be accessed by name but the whole field can be read or written to as well.

    Code:
    typedef union bitfield
    {
       unsigned char value;
       // each member is one bit in 'value'
       struct
       {
          int A : 1;
          int B : 1;
          int C : 1;
          int D : 1;
          int E : 1;
          int F : 1;
          int G : 1;
          int H : 1;
       };
       
    } bitfield_t;
    
    
    
    int main()
    {
       bitfield_t b;
    
       // read from file, network, etc...
       b.value = 0xAC;
    
       if (b.A) // instead of (if (b & (1 << A_FIELD))
       {
            //whatever
       }
       
    
       return 0;
       
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: How Unions will be used practically in C?

    Thanks chemical nova, i am agreeing with you. i also could not find other exapmples.
    Thanks sun burnt, my faculty memeber also told an example which resembles like your one. but at that time i could not concentrate on that. now i cam to an understand..

    Thanks All,
    regards:
    raghunadhs.

    [QUOTE=sunburnt]A nifty trick with unions is to create a bit field where each bit can be accessed by name but the whole field can be read or written to as well.

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: How Unions will be used practically in C?

    Quote Originally Posted by SpS
    Unions also provide crude polymorphism.
    If you are using it like that then your code is seriously flawed.
    I don't live here any more.

  10. #10
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: [RESOLVED] How Unions will be used practically in C?

    He's not.. its a direct quote from Wiki

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] How Unions will be used practically in C?

    Well I didn't see the quote tag... oh there isn't one. Funny that.

    Written by someone that doesn't understand polymorphism and plagiarised by a kindred spirit
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width