Use of unions

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.
 
Q

qqqmac

Here is slightly different example:

double doub(s)
char *s;
{
union {
double d;
unsigned char c[sizeof(double)];
} r;
int i;

for (i=0;i<sizeof(double);i++) {
r.c = (char *)*(s+i);
}
return r.d;
}
 
K

Keith Thompson

Here is slightly different example:

Of what? Read said:
double doub(s)
char *s;

This is an old-style declaration. It's better written as:

double doub(char *s)
{
union {
double d;
unsigned char c[sizeof(double)];
} r;
int i;

for (i=0;i<sizeof(double);i++) {
r.c = (char *)*(s+i);


r.c is of type unsigned char. *(s+i) (better written as s) is
also of type char -- but you're converting the char value to char*.

Your compiler should have warned you about this.

Even without the cast, you're mixing char and unsigned char.
}
return r.d;
}

When you posted to comp.std.c, I advised you to post a *complete*
compilable program that illustrates what you're asking about, and to
explain just what you're trying to accomplish.

It looks like a simple call to memcpy() might accomplish the same
thing, but I can't be sure without knowing what you're trying to do.

Are you trying to understand unions (and using this as an example), or
are you trying to accomplish some specific goal (and using a union as
the solution)? If it's the latter, a union probably isn't the best
approach.
 
Q

qqqmac

The answer is this code is legacy (working) code, and I am going
through it looking at use of unions and trying to discover whether it
is sensible application of unions, or whether it should be re-written.
To your question as to whether I am trying to understand unions, the
answer is yes. Until I read the books I referred to above I thought
you could just read characters from a file (for example) into a
character array, and then "view" this character array as a complex data
structure (If this means anything to you, rather like "redefines" in
Cobol or "equivalence" in Fortran). This is in fact is what is done
numerous times in the the library of C code I am dealing with. I'd
rather not send complete programs to this group as I could be
infringing copyright rules.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top