Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Use of unions

Reply
Thread Tools

Use of unions

 
 
qqqmac@hotmail.com
Guest
Posts: n/a
 
      03-29-2006
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[i]) );
}

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.

 
Reply With Quote
 
 
 
 
qqqmac@hotmail.com
Guest
Posts: n/a
 
      03-29-2006
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[i] = (char *)*(s+i);
}
return r.d;
}

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> Here is slightly different example:


Of what? Read <http://cfaj.freeshell.org/google/>.

> 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[i] = (char *)*(s+i);


r.c[i] is of type unsigned char. *(s+i) (better written as s[i]) 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
qqqmac@hotmail.com
Guest
Posts: n/a
 
      03-29-2006
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-29-2006
(E-Mail Removed) writes:
> The answer is this code is legacy (working) code, and I am going

[snip]

The answer to what?

Read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Safe use of unions dingoatemydonut@aol.com C Programming 4 07-09-2006 02:01 AM
Unions in Swig hemanth Java 1 09-26-2005 08:51 AM
Good use for Unions Michael C++ 11 01-26-2005 08:48 PM
Unique Records from Unions Gadrin77 XML 2 05-18-2004 02:05 AM
unions billyw MCSE 15 01-22-2004 08:11 PM



Advertisments