1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Unions problem

Discussion in 'C' started by mahesh113, Aug 12, 2011.

  1. mahesh113

    mahesh113 New Member

    Joined:
    Aug 12, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    union fun
    {
    char c;
    int i;
    double d;
    };

    If a function set one of the member of fun, but we don't know which member is set as we can see the function call and how can we know after calling that function, that which member of union fun has been set by that function call.

    please tell me and admin please don't delete my post this time.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Which post did admin delete? It wasn't this one: http://www.go4expert.com/showthread.php?t=26487

    If a post was deleted then you would have been given a reason why. If not then maybe what you posted was against the posting guidelines. Certainly duplicate posting is against the guidelines and if your posts have been deleted once and you're still breaking the rules then you risk getting banned. So READ the posting guidelines NOW - they are not difficult to find; there is a link immediately under the New Thread subject line. And make sure you DO NOT break the rules again.


    There is no way to know which member was set. There needs to be some record which indicates what was changed, for example
    Code:
    struct cell
    {
      int type; // 0 for undefined, 1 for char, 2 for int, 3 for double
      union fun
      {
        char c;
        int i;
        double d;
      };
    };
    
    void p(struct cell *c)
    {
      c->type=2;
      c->fun.i=7;
    }
    
    and by inspecting type we can determine that it was i that was set.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Deleted now.
     

Share This Page