An interesting code about "struct" in "union"

This is a discussion on An interesting code about "struct" in "union" within the C++ Programming forums, part of the General Programming Boards category; Code: union{ struct st{ char c; char d; short s; }token; int i; }u; int main(){ u.i=0xff00dd01; char c = ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    An interesting code about "struct" in "union"

    Code:
    union{
    	struct st{
    		char	c;
    		char d;	
    		short s;
    	}token;
    	int i;
    }u;
    int main(){
    	u.i=0xff00dd01;
    	char c = u.token.c;
    	cout<<sizeof(u)<<endl;
    }
    My questions are:
    1. why sizeof(u) is 4? I think it shall be 8 right?
    2. What is c's value after execution?

    thanks

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    1. why sizeof(u) is 4? I think it shall be 8 right?
    Why should it be? An int is 4, and char is 1 (*2), short is 2, so the struct also adds up to 4.

    2. What is c's value after execution?
    0x1, assuming your machine is little-endian (therefore the least significant byte of i is stored first, overlapping with c)

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    2) c's value is green. Or marmelade. It depends on your system.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A union makes all members share the same memory, only with different representations. I've used this for binary readers as an example (to read structured data):
    Code:
    class CPointer
    {
      public:
        union
        {
          void* Void;
          char* Char;
          short* Short;
          int* Int;
          float* Float;
        }
    };
    
    CPointer Pointer;
    
    Pointer.Char = PointToDataSomwhere();
    
    int IntVal1 = *Pointer.Int++;
    int IntVal2 = *Pointer.Int++;
    float FloatVal1 = *Pointer.Float++;
    int IntVal3 = *Pointer.Int++;
    float FloatVal2 = *Pointer.Float++;
    float FloatVal3 = *Pointer.Float++;
    short ShortVal1 = *Pointer.Short++;
    int IntVal4 = *Pointer.Int++;
    (forgive errors, I'm a C# user nowadays)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2007, 12:36 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21