On 2006-04-19, balasam <bk*****@gmail.com> wrote:
I want what is the application of union in C.Please explain
They're quite often used for a sort of "polymorphism".
For example, if you were writing an interpreter for a dynamically typed
language, you might represent values in the language using unions like
this:
enum value_type
{
INT,
REAL,
STRING,
OBJECT
};
struct value
{
enum value_type type;
union
{
int val_int;
double val_real;
char *val_string;
struct object *val_object;
}
value;
};
All values are instances of "struct value", but that can represent an
int, a double, a char or an object.