Thread: Using sizeof on union

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Using sizeof on union

    if I have the union in a struct as below

    Code:
    typedef struct{
       union{
          uint8_t    value;
       }u;
    }s;
    if i want to find the size of the variable value, do i do this?

    Code:
    s testStruct;
    
    sizeof(testStruct.u.value);
    does this return the size which is 1 byte?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    sizeof(testStruct.u.value) is equivalent to sizeof(uint8_t), because testStruct.u.value is of type uint8_t. You could have tested that (admittedly not definitively) using a compiler.

    I see little point in having a struct containing a single union that has a single member, but that's another story.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Edelweiss View Post
    does this return the size which is 1 byte?
    Why didn't you just compile it and find out?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Edelweiss View Post
    if I have the union in a struct as below

    Code:
    typedef struct{
       union{
          uint8_t    value;
       }u;
    }s;
    if i want to find the size of the variable value, do i do this?

    Code:
    s testStruct;
    
    sizeof(testStruct.u.value);
    does this return the size which is 1 byte?
    Well, y'know what... there is one quick and simple way to find that out...

    Compile it and try it....

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Well, y'know what... there is one quick and simple way to find that out...

    Compile it and try it....
    Oh, and I suppose next you are going to tell him to look it up in his helpfile.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  2. Union -Need Ur Help
    By ganesh bala in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 04:57 AM
  3. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  4. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  5. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM