Hi Shiva,

There is no as such way to know what type of value is stored in the Union
(as per my knowledge)
But you can try the following code in order to keep track.

enum { INT, FLOAT, CHAR } uType;

union UserData
{
     uType m_dataType;
     int m_intValue;
     float m_floatValue;
     char m_charValue;
}; data

So whoever update the UserData union he should update the uType variable as
well and while accessing the value following trick can be used:


if (m_charValue.m_dataType == INT)
       printf("%d\n", data.m_intValue);
   if (m_charValue.m_dataType == FLOAT)
       printf("%f\n", data.m_floatValue);
   if (m_charValue.m_dataType == CHAR)
       printf("%c\n", data.m_charValue);
   else
       printf("bad type %d in utype\n", utype);



I hope this will solve your case.


Thanks & Regards,
Rajiv Podar


On Tue, Feb 22, 2011 at 12:15 PM, shiva <[email protected]>wrote:

> I have an union with following members
>
> union data
> {
> int age;
> char grade;
> }var;
>
> now after some operation which assign value to var(it can assign it
> either age or grade), is there any way i can identify whether var
> contain age or grade now.
>
> Thanks for the comments..
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to