Q
qqqmac
FILE file;
union
{
long l;
char c[8];
} r;
long x;
int i;
for (i = 0; i < sizeof(long); i++)
{
res = fscanf( file, "%c", &(r.c) );
}
x = l;
Is the code fragment above an example of correct use of a union? I
have read books (1,2) that say the assignment to x is incorrect as the
component last used before this assignment is the character array, not
the long component. I ask because this technique (using data
structures as components rather than primitives) is used quite a lot in
some of our older C code.
1. The C Programming Language second edition (Kernighan and Ritchie),
1988
2. A C Reference Manual (Harbison and Steele), 2002.
Thanks for any advice you can give.
union
{
long l;
char c[8];
} r;
long x;
int i;
for (i = 0; i < sizeof(long); i++)
{
res = fscanf( file, "%c", &(r.c) );
}
x = l;
Is the code fragment above an example of correct use of a union? I
have read books (1,2) that say the assignment to x is incorrect as the
component last used before this assignment is the character array, not
the long component. I ask because this technique (using data
structures as components rather than primitives) is used quite a lot in
some of our older C code.
1. The C Programming Language second edition (Kernighan and Ritchie),
1988
2. A C Reference Manual (Harbison and Steele), 2002.
Thanks for any advice you can give.