B
balasam
I want what is the application of union in C.Please explain
samrox said:U really need to read up a C book mate...its quite a stupid question!
Dear friend,samrox said:U really need to read up a C book mate...its quite a stupid question!
Newton was right, earth sucks!
here is a very simple example to explain the use of unions in C
union test
{
int x;
char c;
};
int main()
{
union test t;
t.x=65;
printf("\n%c\n",t.c);
return 0;
}
output:
A
Naresh said:Can we use floats also? I know it is stored as exponent and mantissa in
bytes. But I dont know if it can be use (or is valid in C standard) in
union also. plz guide.
Naresh said:Can we use floats also? I know it is stored as exponent and mantissa in
bytes. But I dont know if it can be use (or is valid in C standard) in
union also. plz guide.
Abdo said:Of course, however you need to be careful... As you mentioned floats
are represented as exponent and mantissa, unlike integers which are
represented using the 2's complement. Consider a union like this one:
union
{
int iFloatBits;
float fSomeNumber;
}
If you check the value of iFloatBits, it won't be the integer part of
fSomeNumber, but rather the integer number which is produced by the
bits of fSomeNumber...
Vladimir said:As an aside, unless you have a very good specific reason, use `double`
instead of `float`.
in Msg. said:union
{
char b[SIZEOF_DOUBLE];
double d;
}
Where SIZEOF_DOUBLE is, obviously == `sizeof(double)`.
here is a very simple example to explain the use of unions in C
union test
{
int x;
char c;
};
int main()
{
union test t;
t.x=65;
printf("\n%c\n",t.c);
return 0;
}
output:
A
now here's what is happening
storage space is provided for the largest datatype of the union.
in our example it is x..(int -4by and char -1by)
when we assign 65 to x the binary equivalent of 65 is stored in memory.
the char equivalent of 65 is A
hence when we try to access c the char 'A' is printed to the screen.
memory allocation:
int
byte1 byte 2 byte 3 byte 4
-------- -------- -------- -------- => 65
00000000 00000000 00000000 01000001
char
-------- => A
01000001
Abdo said:Any logical reason for this? I always use floats, especially if I don't
need the extra precision of double, also many third part libraries
(Like DirectX and OpenGL) use them...
Vladimir S. Oka said:Probably the best way to explore bit-representation of `float`/`double`
is to use a `union` with a `char` array:
union
{
char b[SIZEOF_DOUBLE];
double d;
}
Where SIZEOF_DOUBLE is, obviously == `sizeof(double)`.
Reading out values of `b[index]` is always OK, as `char` cannot have
trap representation. Be careful if you want to experiment by writing to
`b[index]`, as it may create a trap representation of `d`. This will be
a problem once `d` is read (provided it assumed a trap value).
Vladimir S. Oka said:Probably the best way to explore bit-representation of
`float`/`double` is to use a `union` with a `char` array:
union
{
char b[SIZEOF_DOUBLE];
double d;
}
Where SIZEOF_DOUBLE is, obviously == `sizeof(double)`.
Reading out values of `b[index]` is always OK, as `char` cannot have
trap representation. Be careful if you want to experiment by writing
to `b[index]`, as it may create a trap representation of `d`. This
will be a problem once `d` is read (provided it assumed a trap
value).
It's much better to use unsigned char rather than char (which can be
either signed or unsigned). I think you're right that char can't
have trap representations, but it can have padding bits; unsigned
char cannot.
In general, storing a value in one member of a union and then reading
another member is dangerous unless the member you read is an array of
unsigned char. Using a union to overlay the representations of two
different types is common, but it's not really supported by the
standard.
Another common use of unions is to implement something like Pascal's
variant records. For example:
enum type { INT, DOUBLE, POINTER };
struct variant {
enum type current_type;
union {
int i;
double d;
void *p;
}
}
The value of the current_type member indicates which union member is
currently active.
Your "1" key is sticking.balasam said:Dear friend,
I studied the second edition of C programming language that is
wirtten by "Kernigan" and "Dennis Ritchie".I studied the union is
mainly used in a compiler symbol table and It is also used in printf
and scanf library function,etc.,.Please note i want other than these
points and important application of UNION.
Thanks for your reply!!!!!!!!!!!!!
bye bye!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
balasam said:I want what is the application of union in C.Please explain
typedef union
{
int int_member;
long long_member;
double double_member;
} cool_union;
typedef struct
{
int int_member;
long long_member;
double double_member;
} cool_struct;
cool_union x;
cool_struct y;
sizeof (x) = sizeof (double);
but
sizeof (y) = sizeof (double) + sizeof (long) + sizeof (int).
regis said:The 8 bytes case happens when ordering field sizes:
- by increasing size: char, (1 padding), int, long
- by decreasing size: long, int, char, (1 padding)
The 12 bytes case happens with nonmonotonic field size ordering,
e.g:
- char, (3 padding), int, (2 padding), long
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.