Thread: union in struct with bit fields

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    union in struct with bit fields

    hi ,
    I am having bit field packed in union of struct..
    but not able to retrieve values correctly
    /..........

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    3
    My code is
    My code is
    insert
    Code:
    #include <string.h>
    #include <stdio.h>
    
    typedef union
            {
            unsigned dd:4;
            unsigned ee;
    
            }ff;
    
    typedef struct {
            int unittype:2;
            ff ss;
            union {
            int units:3;
            } howmuch ;
            //amount howmuch;
            } product;
    int main()
    {
    
            product dieselmotorbike;
    
            product * myebaystore[2];
            int nitems = 1; int i;
    
            dieselmotorbike.unittype = 1;
    
            dieselmotorbike.howmuch.units = 3;
    
            dieselmotorbike.ss.dd = 14;
            dieselmotorbike.ss.ee = 4;
    
    
            myebaystore[0] = &dieselmotorbike;
    
            for (i=0; i<nitems; i++) {
                    switch (myebaystore[i]->unittype) {
                    case 1:
                                    printf("We have &#37;u units for sale kg \n %u      ",
                                    myebaystore[i]->howmuch.units,
                                    myebaystore[i]->ss.dd,
                                    myebaystore[i]->ss.ee);
    
                            break;
                    case 2:
                            printf("We have %f kgs for sale\n",
                                    myebaystore[i]->howmuch.units);
                            break;
                    }
            }
    }
    
    [code
    If i print the values they print last updated element ...........
    Please suggest the remedy..........


    Sagar...

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure what you expect, but certainly this:
    Code:
    typedef union
            {
            unsigned dd:4;
            unsigned ee;
    
            }ff;
    is one field, which you can either use as a 4-bit field or a (presumably) 32-bit. ee and dd will cover the same piece of memory (but of course dd is bigger, so it will cover some memory that is not covered by dd).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    3
    in the program if i assigned value to my dd and ee separetely they should print their own values but still they are giving last eneterd value..

    What do you mean by
    " ee and dd will cover the same piece of memory (but of course dd is bigger, so it will cover some memory that is not covered by dd). "

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sagarnewase View Post
    in the program if i assigned value to my dd and ee separetely they should print their own values but still they are giving last eneterd value..
    My turn to ask what you mean?

    What do you mean by
    " ee and dd will cover the same piece of memory (but of course dd is bigger, so it will cover some memory that is not covered by dd). "
    I mean that ee and dd overlap each other in the same location of memory.

    Consider this:
    Code:
    union a
    {
        int x;
        int y;
    };
    Now, x and y will have exactly the same memory address, and if we write this:
    Code:
    union a aa;
    
    ...
    aa.x = 7;
    aa.y = 8;
    printf("aa.x = %d\n", aa.x);
    This will produce 8 as the output.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Accessing bit fields using a union problem.
    By samdomville in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 04:39 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Union in a struct?
    By Hunter2 in forum C Programming
    Replies: 2
    Last Post: 06-13-2003, 11:45 AM
  5. Class + Union + Struct = Disaster??
    By Inquirer in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 03:55 PM