Use of unions

Discussion in 'C Programming' started by qqqmac@hotmail.com, Mar 29, 2006.

  1. Guest

    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.
     
    , Mar 29, 2006
    #1
    1. Advertising

  2. Guest

    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;
    }
     
    , Mar 29, 2006
    #2
    1. Advertising

  3. 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 = (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.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Mar 29, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. 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) <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.
     
    Keith Thompson, Mar 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hemanth

    Unions in Swig

    hemanth, Sep 26, 2005, in forum: Java
    Replies:
    1
    Views:
    544
    Roedy Green
    Sep 26, 2005
  2. Gadrin77

    Unique Records from Unions

    Gadrin77, May 13, 2004, in forum: XML
    Replies:
    2
    Views:
    461
    Gadrin77
    May 18, 2004
  3. =?iso-8859-1?Q?Juli=E1n?= Albo

    Members of anonymous unions in intialization lists

    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 25, 2003, in forum: C++
    Replies:
    1
    Views:
    338
    Victor Bazarov
    Nov 25, 2003
  4. Michael

    Good use for Unions

    Michael, Jan 24, 2005, in forum: C++
    Replies:
    11
    Views:
    697
    Aslan Kral
    Jan 26, 2005
  5. Safe use of unions

    , Jun 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    292
    Robert Gamble
    Jul 9, 2006
Loading...

Share This Page