union inside structure

This is a discussion on union inside structure within the C Programming forums, part of the General Programming Boards category; where union inside structure is used.... as i know structure inside union is used when we need to access the ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    union inside structure

    where union inside structure is used....

    as i know structure inside union is used when we need to access the variable of abstract type and also its lower part ...
    ex.., individual bit of integer and whole integer can be accessed with this.

    but where union inside structure is used... this is interview question
    (i'm an embedded software professional... expecting an answer in embedded point of view)

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    This is what I found in my book.
    Suppose we wish to store information about employees in an organization.
    Name Grade Age
    If grade=HSK(Highly skilled)
    hobby name
    credit card number
    If Grade=SSK(Semi skilled)
    Vehicle no.
    Distance from Company
    We can use a single structure for it but then it would lead to wastage of memory coz either hobby name & credit card no. or vehicle no. & distance from com. is used at a time. Both of them are never used simultaneously. So, here union inside structure can be used effectively:
    Code:
    struct info1
    {
    char hobby[10];
    int crcardno;
    };
    struct info2
    {
    char vehno[10];
    int dist;
    };
    union info
    {
    struct info1 a;
    struct info2 b;
    };
    struct emp
    {
    char n[20];
    char grade[4];
    int age;
    union info f;
    };
    struct emp e;
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with pointer to structure
    By dp2452 in forum C Programming
    Replies: 5
    Last Post: 11-19-2009, 08:44 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  3. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Using malloc() & qsort with a union
    By Cyber Kitten in forum C Programming
    Replies: 2
    Last Post: 09-02-2001, 07:57 AM

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