Results 1 to 4 of 4

Thread: union

  1. #1
    Junior Member
    Join Date
    May 2008
    Answers
    8

    union

    wat is d real time application of union....


  2. #2
    Expert Member
    Join Date
    Apr 2008
    Answers
    1,859

    Re: union

    hi friend

    A "union declaration" specifies a set of variable values and, optionally, a tag naming the union. The variable values are called "members" of the union and can have different types. Unions are similar to "variant records" in other languages

    Thanks
    Deepasree


  3. #3
    Junior Member
    Join Date
    Oct 2008
    Answers
    12

    Re: union

    A union is a variable typed by the keyword union, which may hold at different types and sizes. Union provides a way to manipulate different kinds of data ina single storage, ie. the purpose of union is to provide a single variable, which can hold any 1 of the several types, defined.
    Unions are useful for applications involving, multiple members, where values need not b assigned to all the members at any instant.


  4. #4
    Contributing Member
    Join Date
    Dec 2007
    Answers
    46

    Re: union

    Quote Originally Posted by Ayesha Siddiqua View Post
    A union is a variable typed by the keyword union, which may hold at different types and sizes. Union provides a way to manipulate different kinds of data ina single storage, ie. the purpose of union is to provide a single variable, which can hold any 1 of the several types, defined.
    Unions are useful for applications involving, multiple members, where values need not b assigned to all the members at any instant.
    You are right.

    A practical example is listed below.

    Let us say that there are two types of blocks.
    blk_type == A; dependent data is an integer
    blk_type == B; dependent data is a char array of 4 bytes.

    We can define the structures:

    struct blkA {
    int a;
    }

    struct blkB {
    char b[4];
    }

    But, this would mean that all functions now have to deal with two different block types: different prototypes, etc. Leads to lot of code bloat.

    We could do better like below:

    struct block {
    blk_type;
    int a;
    char b[4];
    }

    But, this would lead to wastage of space. How?? Total space allocated is 8 bytes, apart from block type. When block type is A, we will not use b[4], and when block type is B, we will not use int a. So, at any time, 4 bytes are wasted.

    We can do even better by using unions.

    struct block {
    blk_type;
    union {
    int a;
    char b[4];
    };
    };

    The advantage of a union is that space is allocated only once for the highest sized member of the union. In our example above, we will allocate only 4 bytes (assuming int is 4 bytes). And the same 4 bytes is used whether it is block type A or B. And, apart from savings in space, we can have one routine dealing with both block types, depend on the block type field.

    Hope that helps.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact