Unions problem

mahesh113's Avatar, Join Date: Aug 2011
Newbie Member
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Deleted now.