473,383 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Use of unions

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.

Mar 29 '06 #1
4 1344
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;
}

Mar 29 '06 #2
qq****@hotmail.com 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) ks***@mib.org <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.
Mar 29 '06 #3
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.

Mar 29 '06 #4
qq****@hotmail.com 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) ks***@mib.org <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.
Mar 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
8
by: SteveM | last post by:
The general consensus I am getting is that nobody really uses unions much (engineers here at work) but this is an academic exercise for me so I am looking for an answer (I know there may be better...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
4
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
26
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.