structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anarghya
    New Member
    • Oct 2006
    • 18

    structure

    how a structure is different from union?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by anarghya
    how a structure is different from union?
    A union is similar to a structure in the way that it is declared and how its members are accessed, but there is a fundamental difference in the way storage is allocated:

    structures: each member has its own area of storage
    unions: the members use the same area of storage, i.e. they overlay each other.

    Comment

    • baburk
      New Member
      • Oct 2006
      • 111

      #3
      At a time u can store only one value in union. Because, all variables occupy the same memory. If u store 2 values it will affect the other variable.

      Comment

      • koder
        New Member
        • Sep 2006
        • 23

        #4
        consider the code below,

        Code:
        #include<stdio.h>
        union koder {
        
                          char c;
                          int i;
                          double d;
                      };
        
        main()
         {
           union  koder  new;
           printf("\n %d",sizeof(new));
        }
        #
        Code:
        #include<stdio.h
        union koder {
        
                          char c;
                          int i;
                          double d;
                      };
        main()
         {
            union  koder  new;
           printf("\n %d",sizeof(new));
        
        }
        first code peice shows a union,when u print the size of the union it will give u the size of the largest member present in union .so at a time only any one member of the union can be accessed
        But in second case the size of the structure gives u ,the 16 bytes(with gcc obvious) This includes the padding considerations, so equall to sum of sizes of all the element present.Thus we can understand that in a structure we can all elements or a single element.

        Comment

        • koder
          New Member
          • Sep 2006
          • 23

          #5
          i mean we can access single element or all elements at a given time
          Thanks

          Comment

          Working...