Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between structure and union in c

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 308
    Comment on it

    Structure

     

    Union

     

    The keyword struct is used to define a structure.

    The keyword union is used to define a union.

     

    In structure each member has its own storage location.

    All members of union uses a single shared memory location which is equal to the size of its largest data member.

     

    Changing the value of a member will not affect other members of the structure.

     

    Changing the value of any of the member will Change other member values.

     

    Individual member can be accessed at a time.

     

    Only one member can be accessed at a time.

     

    Any number of members can initialize at once.

     

    Only the first member of a union can be initialized.

     

    All variables can be directly accessed as each member has its own storage location.

    Only the last entered variable can be directly accessed as only the last added data to the union will exist in the memory.

    For Example:-

    struct emp

    {

    char A; //size 1 byte

    float B; //size 4 byte

    }e;

    Total number of bytes allocated are 5.

    For Example:-

    union emp

    {

    char A;

    float B;

    }e;

    Total number of bytes allocated are 4.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: