Thread: little bit confused on union

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    59

    little bit confused on union

    Hello!

    I have recently read a section of union.
    My attempt
    Code:
    #include<stdio.h>
    
    union UnionName 
    { 
      int x; 
      int y; 
      int z;
      
     }u1;
     
    int main ()
    {
        u1.x = 30;
        u1.y = 20;
        u1.z = 10;
    
    
        printf ("\n x : %d ", u1.x);
        printf ("\n y : %d ", u1.y);
        printf ("\n z : %d ", u1.z);
    
    
        printf ("\n Location of x : %d ", &u1.x);
        printf ("\n Location of y : %d ", &u1.y);
        printf ("\n Location of z : %d ", &u1.z);
    
    
        printf ("\n size of union : %d ", sizeof(u1)); 
    	
        return 0;
    }
    x : 10
    y : 10
    z : 10

    Location of x : 4223088
    Location of y : 4223088
    Location of z : 4223088

    size of union : 4

    compiler allocate only one memory location 4223088
    first member of union will store at memory location 4223088
    second member of union will store at memory location 4223088
    third member of union will store at memory location 4223088


    Each member share the same memory in union while compiler allocate memory for every structure member

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    817
    What part confuses you? This looks like what one would expect. Do you know that an int takes 4 bytes╣ of memory? So the union only takes 4 bytes even though it contains 3 ints. If it was a regular struct, it would take 12 bytes!

    hush!

  3. #3
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    31
    Hello varja11!

    If you want to print the address of a variable it is better tu use '%p' instead of 'd'
    All three variables of int have the same size, so they starts at the same place in memory.
    If you would change your code like this:
    Code:
    #include <stdio.h>
    
    union UnionName 
    { 
      int x; 
      int y; 
      int z;
      unsigned char parts[4];
       
     }u1;
      
    int main(int argc, char **argv)
    {
        u1.x = 30;
        u1.y = 20;
        u1.z = 10;
     
     
        printf ("\n x : %d ", u1.x);
        printf ("\n y : %d ", u1.y);
        printf ("\n z : %d ", u1.z);
     
     
        printf ("\n Location of x : %p ", &u1.x);
        printf ("\n Location of y : %p ", &u1.y);
        printf ("\n Location of z : %p\n", &u1.z);
     
        printf ("\n parts[0] : %d ", u1.parts[0]);
        printf ("\n parts[1] : %d ", u1.parts[1]);
        printf ("\n parts[2] : %d ", u1.parts[2]);
        printf ("\n parts[3] : %d\n", u1.parts[3]);
     
        printf ("\n Location of parts[0] : %p ", &u1.parts[0]);
        printf ("\n Location of parts[1] : %p ", &u1.parts[1]);
        printf ("\n Location of parts[2] : %p ", &u1.parts[2]);   
        printf ("\n Location of parts[3] : %p ", &u1.parts[3]);
     
        printf ("\n size of union : %lu\n", sizeof(u1)); 
         
        return 0;
    }
    You will have different address for u1.parts[]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Union -Need Ur Help
    By ganesh bala in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 04:57 AM
  2. union
    By studentc in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 01:37 PM
  3. What is a union?
    By ammar in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2002, 03:36 AM
  4. if union could...
    By black in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2002, 08:34 AM
  5. Union
    By {Dnw} in forum C Programming
    Replies: 7
    Last Post: 08-18-2001, 12:28 AM

Tags for this Thread