generally of different types, at different times. Declaring a union is similar to
declaring a structure. Its general form is
declaring a structure. Its general form is
union union-type-name {
type member-name;
type member-name;
type member-name;
} union-variables;
For example:
union u_type {
int i;
char ch;
};
This declaration does not create any variables. You may declare a variable either
by placing its name at the end of the declaration or by using a separate declaration
statement. In C, to declare a union variable called cnvt of type u_type using the
definition just given, write
union u_type cnvt;
When declaring union variables in C++, you need use only the type name—
you don't need to precede it with the keyword union. For example, this is how
cnvt is declared in C++:
u_type cnvt;
In C++, preceding this declaration with the keyword union is allowed, but redundant.
In C++, the name of a union defines a complete type name. In C, a union name is its
tag and it must be preceded by the keyword union. (This is similar to the situation
with structures described earlier.) However, since the programs in this chapter are
valid for both C and C++, the C-style declaration form will be used.
In cnvt, both integer i and character ch share the same memory location. Of
course, i occupies 2 bytes (assuming 2-byte integers) and ch uses only 1. Figure 7-2
shows how i and ch share the same address. At any point in your program, you can
refer to the data stored in a cnvt as either an integer or a character.
When a union variable is declared, the compiler automatically allocates enough
storage to hold the largest member of the union. For example (assuming 2-byte
integers), cnvt is 2 bytes long so that it can hold i, even though ch requires only
1 byte.
To access a member of a union, use the same syntax that you would use for
structures: the dot and arrow operators. If you are operating on the union directly,
use the dot operator. If the union is accessed through a pointer, use the arrow
operator. For example, to assign the integer 10 to element i of cnvt, write
cnvt.i = 10;
In the next example, a pointer to cnvt is passed to a function:
void func1(union u_type *un)
{
un->i = 10; /* assign 10 to cnvt using
function */
}
Using a union can aid in the production of machine-independent (portable)
code. Because the compiler keeps track of the actual sizes of the union members,
no unnecessary machine dependencies are produced. That is, you need not worry
about the size of an int, long, float, or whatever.
Unions are used frequently when specialized type conversions are needed
because you can refer to the data held in the union in fundamentally different
ways. For example, you may use a union to manipulate the bytes that comprise a
double in order to alter its precision or to perform some unusual type of rounding.To get an idea of the usefulness of a union when nonstandard type conversions
are needed, consider the problem of writing a short integer to a disk file. The C/C++
standard library defines no function specifically designed to write a short integer to
a file. While you can write any type of data to a file using fwrite(), using fwrite()
incurs excessive overhead for such a simple operation. However, using a union you
can easily create a function called putw() , which writes the binary representation of
a short integer to a file one byte at a time. (This example assumes that short integers
are 2 bytes long.) To see how, first create a union consisting of one short integer and
a 2-byte character array:
union pw {
short int i;
char ch[2];
};
Now, you can use pw to create the version of putw() shown in the following program.
#include <stdio.h>
union pw {
short int i;
char ch[2];
};
int putw(short int num, FILE *fp);
int main(void)
{
FILE *fp;
fp = fopen("test.tmp", "wb+");
putw(1000, fp); /* write the value 1000 as an integer */
fclose(fp);
return 0;
}
int putw(short int num, FILE *fp)
{
union pw word;word.i = num;
putc(word.ch[0], fp); /* write first half */
return putc(word.ch[1], fp); /* write second half */
}
Although putw() is called with a short integer, it can still use the standard function
putc() to write each byte in the integer to a disk file one byte at a time.
structures: the dot and arrow operators. If you are operating on the union directly,
use the dot operator. If the union is accessed through a pointer, use the arrow
operator. For example, to assign the integer 10 to element i of cnvt, write
cnvt.i = 10;
In the next example, a pointer to cnvt is passed to a function:
void func1(union u_type *un)
{
un->i = 10; /* assign 10 to cnvt using
function */
}
Using a union can aid in the production of machine-independent (portable)
code. Because the compiler keeps track of the actual sizes of the union members,
no unnecessary machine dependencies are produced. That is, you need not worry
about the size of an int, long, float, or whatever.
Unions are used frequently when specialized type conversions are needed
because you can refer to the data held in the union in fundamentally different
ways. For example, you may use a union to manipulate the bytes that comprise a
double in order to alter its precision or to perform some unusual type of rounding.To get an idea of the usefulness of a union when nonstandard type conversions
are needed, consider the problem of writing a short integer to a disk file. The C/C++
standard library defines no function specifically designed to write a short integer to
a file. While you can write any type of data to a file using fwrite(), using fwrite()
incurs excessive overhead for such a simple operation. However, using a union you
can easily create a function called putw() , which writes the binary representation of
a short integer to a file one byte at a time. (This example assumes that short integers
are 2 bytes long.) To see how, first create a union consisting of one short integer and
a 2-byte character array:
union pw {
short int i;
char ch[2];
};
Now, you can use pw to create the version of putw() shown in the following program.
#include <stdio.h>
union pw {
short int i;
char ch[2];
};
int putw(short int num, FILE *fp);
int main(void)
{
FILE *fp;
fp = fopen("test.tmp", "wb+");
putw(1000, fp); /* write the value 1000 as an integer */
fclose(fp);
return 0;
}
int putw(short int num, FILE *fp)
{
union pw word;word.i = num;
putc(word.ch[0], fp); /* write first half */
return putc(word.ch[1], fp); /* write second half */
}
Although putw() is called with a short integer, it can still use the standard function
putc() to write each byte in the integer to a disk file one byte at a time.
**Note :C++ supports a special type of union called an anonymous union which is
discussed in Part Two of this book.
discussed in Part Two of this book.
No comments:
Post a Comment